Forum Moderators: coopster

Message Too Old, No Replies

Insert Records to MySQL

What am I missing from my script?

         

joe1182

12:39 am on Dec 8, 2004 (gmt 0)

10+ Year Member



When I use the script below it doesn't insert the record into MYSQL database. I have checked the fields in the form and they are showing fine and the database is connecting without a problem. Does anyone see anything wrong with my script? Any help would be greatly appreciated.

$sql = mysql_query("INSERT INTO customers ('client name', 'bill to address', 'shipping address', 'phone', 'contact name') VALUES ('$clientname', '$billtoaddress', '$shippingaddress', '$phone', '$contactname')");

Birdman

1:14 am on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The first step to trouble-shooting MySql errors, is to print the actual error message.

$sql = mysql_query("INSERT INTO customers...") or die(mysql_error());

Upon error, you will get a message to help you debug.

joe1182

1:57 am on Dec 8, 2004 (gmt 0)

10+ Year Member



You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ''client name', 'bill to address', 'shipping address', 'phone',

This is the error message I receive when running the following script. Any ideas?

<?php
$clientname = $_POST['clientname'];
$contactname = $_POST['contactname'];
$phone = $_POST['phone'];
$shippingaddress = $_POST['shippingaddress'];
$billtoaddress = $_POST['billtoaddress'];
$sql1 = mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
$dbh = mysql_select_db ("database");
$sql = mysql_query("INSERT INTO customers ('client name', 'bill to address', 'shipping address', 'phone', 'contact name') VALUES ('$clientname', '$billtoaddress', '$shippingaddress', '$phone', '$contactname')") or die(mysql_error());
print $sql;
?>

joe1182

11:08 am on Dec 8, 2004 (gmt 0)

10+ Year Member



Anyone have any suggestions on this?

Birdman

11:36 am on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I suggest replacing the single quotes with backticks ( ` ) in the field list. In the future, avoid using spaces in names.

$sql = mysql_query("INSERT INTO customers (`client name`, `bill to address`, `shipping address`, `phone`, `contact name`) VALUES ('$clientname', '$billtoaddress', '$shippingaddress', '$phone', '$contactname')");

Cheers

joe1182

1:53 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



Thanks Birdman. That worked!

henry0

2:02 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello Birdman
could you please explain your logic?
thanks

I use no quote or anything else and leave a space after the comma.
As is it works fine
(asasas, asaas, asasa, asas)

Henry

Birdman

2:22 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If your table/field/db names have no spaces and they do not share their names with any reserved MySql functions, you need not use the backticks.

[dev.mysql.com...]

[dev.mysql.com...]