Error: You have an error in your SQL syntax

I keep getting

Error: 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 ‘ID, Product Name, Description, Price, Location) VALUES (’’,’’,’’,’’,’’)’ at line 1
can any one help?
Here is the code:

PHP:
[php]<?php
$con = mysql_connect(“mysql3..com",“a3590170_*****”,"”);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}

mysql_select_db(“a3590170_***”, $con);

$sql=“INSERT INTO oms (Product ID, Product Name, Description, Price, Location)
VALUES
(’$_POST[ProductID]’,’$_POST[ProductName]’,’$_POST[Description]’,’$_POST[Price]’,’$_POST[Location]’)”;

if (!mysql_query($sql,$con))
{
die('Error: ’ . mysql_error());
}
echo “1 record added”;

mysql_close($con);
?> [/php]

HTML:

[code]


Product ID:
Product Name:

Price:
Location:
[/code]

You have a spaces in your column names. I don’t recommend using spaces.

You should enclose all field and table names in backticks. For example:

INSERT INTO `oms` (`Product ID`, `Product Name`, `Description`, `Price`, `Location`)

That’s not the problem though, its the lack of data. None of the information was passed to the second page.

Well no, you can input empty values. If you look at the error it says “near 'ID” where the first space is.

But can u have spaces in column names? It did say near id, but the column name is product id, it thinks prodct and id are two different names.

You can have spaces as long as you enclose with backticks. If Product and ID are two different columns there is still a comma missing. Also if you look at his values… $_POST[ProductID]

Then encasing the column names should fix it then.

I believe so. I think everyone should use backticks even when not necessary. I use them religiously lol

I think i’ve used them once or twice, but i rarely specify which columns i’m populating, its just easier for me not to put spaces in the column names, just easier for me to type :slight_smile:

Thank you :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service