Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot insert a record in database.

Status
Not open for further replies.

Chelsea7

Programmer
Aug 25, 2008
69
US
Hello everyone. I'm trying to insert records in a table using a form in HTML. I also created the php file with the information. But I cannot insert any records to the database. I could connect successfully and I receive no error messages. But the data will not insert to the table. I have admin rights. This is MYSQL Server version: 4.1.14. Here is first the HTML code for the form;

<form name="email_list" action="emailme.php" method="post" >
<font color="#FF0000"> Email</font>:
<input type="text" name="email" size=40'>
Company:
<input type="text" name="company" size=20><br><br>
<font color="#FF0000">First Name</font>:
<input type="text" name="fname" size=15/>
Last Name:
<input type="text" name="lname" size=25/>
<br><br>Street/Box Address:
<input type="text" name="addr" size=35/>
Apt/Unit #:
<input type="text" name="unit" size=4/>
<br><br>City/Providence:
<input type="text" name="ctyname" size=35/>
State:
<input type="text" name="stname" size=2/>
Zip:
<input type="text" name="zip" size=6/><br><br>
Country:
<input type="text" name="country" size=25/>
<p><input type=submit name="submit" value="Submit"><input type="reset" name="Reset" value="Reset">

====================Here is the php file=================
<?
$ID_address=$_POST['id_address'];
$ID=$_POST['ID'];
$company=$_POST['company'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$addr=$_POST['addr'];
$unit=$_POST['unit'];
$ctyname=$_POST['ctyname'];
$email=$_POST['email'];
$stname=$_POST['stname'];
$zip=$_POST['zip'];
$country=$_POST['country'];
$conn=mysql_connect("mysql", "userid", "password") or die(mysql_error());
mysql_select_db("thespianprada",$conn) or die(mysql_error());
mysql_query("INSERT INTO 'contacts' VALUES ('$id_address','$ID','$company','$fname', '$lname','$addr','$unit','$ctyname','$email', '$stname','$zip','country')");
Print "Your information has been successfully added to the database.";


?>

I'm sure what I can't insert in the table. Is there something wrong with the syntax for the INSERT command? I followed some tutorials and still get no results. Any assistance will be apreciated.
 
You say you get no error, but I do not see you checking for it. Use the mysql_errno() funtion to test for it, and if non-zero, the mysql_error() to get the message.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
I suspect that you shouldn't have quote marks around 'contacts'.

Your statement should look like:
Code:
mysql_query("INSERT INTO contacts VALUES ('$id_address','$ID','$company','$fname', '$lname','$addr','$unit','$ctyname','$email', '$stname','$zip','$country')");
Also you appear to be missing a $ before country in your final parameter in the list of VALUES.

As DonQuichote says, you should check for errors but if you have a problem like this it is usually a good idea to print or display the actual SQL command that is causing the error.

Andrew
Hampshire, UK
 
You might also want to check the case on your "$ID_address" vs "$id_address".


Trojan.
 
In fact I have to wonder where you expect "id_address" to be coming from because I can see no evidence of it in your html.

Maybe I'm missing something obvious here ....



Trojan.
 
One other thing, think log files. They are invaluable for feedback from your scripts because even when you "forget" to check return values you'll often find something logged in the log file.

HTH.


Trojan.
 
Hello everyone. I got it to work. I had a few syntax errors in the INSERT INTO fields. Pretty much what everyone was saying. So I just took baby steps and built it up on field at a time which helped me discover the errors.

Thanks everyone!
Chelsea
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top