Solved

Adding database entries with PHP and mSQL

Posted on 2000-03-03
3
188 Views
Last Modified: 2009-07-29
(Note: This is mSQL, rather than MySQL)..


I am getting SOOOOoooo depressed about this, it's not true!!!

All I want to do is to add an entry to a table with data entered into a form. The exact coding is below, but what happenes is that the "Submit" button is recognised (i.e. the "Data entered.." line comes up), but nothing is actually added to the table.

Please, please, please can somebody help???

Anyway, here's the exact code I'm trying to use..

<form>
<table border=0>
<tr>
<td>Country:</td>
<td><input type=text name=country size=20></td>
</tr>
<tr>
<td>Port:</td>
<td><input type=text name=port size=20></td>
</tr>
<tr>
<td>Country ID:</td>
<td><input type=text name=country_id size=20></td>
</tr>
<tr>
<td align=center><input type=submit name="submit" value="Add destination"></td>
<td align=center><input type=reset value="Cancel"></td>
</tr>
</table>
</form>
</center>

<?php
if ($submit=="Add destination") {

msql_connect ("");


$sql="INSERT INTO countries VALUES ($country,$port,$country_id)";

$result=msql ("1sitetest",$sql);

echo "Information added. Refresh this page to update table above\n";
}
?>


</form>
0
Comment
Question by:PoRL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Accepted Solution

by:
sereda earned 100 total points
ID: 2580018
You need to specify
<form action="<?echo $PHP_SELF;?>" method="get">

instead of simply <form>.

Also, advice - do not test value of $submit button. For example, if user enters values then presses ENTER button, then $submit will be empty but the form is still submitted.
Instead, use something like

<input type="hidden" name="formaction" value="submit">

<?
  if ($formaction == "submit") {
....
  }
?>

0
 
LVL 2

Expert Comment

by:maxkir
ID: 2581536
Good comment from sereda :-)

My addition - I'd rewrite your database code in the following way:
Note die() calls, $db connection identifier, 's in $sql statement.

<?php
 if ($submit=="Add destination") {

   // Get link identifier
   $db = msql_connect () or die("Unable to connect to database");

    $sql="INSERT INTO countries VALUES ('$country','$port','$country_id')";

   // Use link identifier in request  
   msql ($db, $sql) or die ("Error inserting data to database. Query: $sql");

    echo "Information added. Refresh this page to update table above\n";
}
?>
0
 

Author Comment

by:PoRL
ID: 2581925
Hmmmm... I thought I'd accepted this answer earlier on!?!

Basically, just after I posted the original question, I got a phone call out of the blue from an old friend in South Africa, and he guided me to where I wanted to be.

I felt the point about the Submit button was worth all the points, though!!!

Thanks for the help...

Paul
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question