Question:
Why does my PHP script give a MySQL syntax error?
HH
2012-06-18 13:48:59 UTC
The insert statement I added:

$name = $_POST['name'];
$city = $_POST['city'];
$country = $_POST['country'];
$email = $_POST['email'];
$desc = $_POST['desc'];

$sql = "INSERT INTO complaint registration (name, city, country, email, desc)
VALUES
('$name', '$city', '$country', '$email', '$desc')";

if (mysql_query($sql)) {
echo "Complain registered succcessfully";
} else {
echo "Complaint not registered.";
}


The 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...."

The Connection with the database was successful. The connection was closed later on in the script. Please help.
Four answers:
Max Demian
2012-06-18 14:02:36 UTC
Try wrapping your table name in back ticks (next to the 1 key - not single quotes).
TheMadProfessor
2012-06-19 13:42:19 UTC
This is why having table or column names that include embedded spaces is a Bad Idea. If you're gonna do that, you have to enclose the entire name in square brackets or the like every single time you reference it.
J0nny3
2012-06-18 13:54:58 UTC
Probably cause you have a space inbetween your table name 'complaint registration', change the table name so it doesn't contain a space
oreskovich
2016-10-28 16:23:09 UTC
comprise 'closedb.phps'; <-- the 's' should not be interior the document extension must be comprise 'closedb.very own domicile page'; additionally your submit variables could could desire to be replaced to apply single expenditures as a replace of double. a minimum of i've got continuously used single expenditures. $_POST["sOrdered_P"]; replace to $_POST[sOrdered_P'];


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...