I am brand new to PHP and brand new to this forum. I have done some tutorials and am now busy trying to make my own pages.
I have a database up and running and trying to add to it with a script.
Here it is:
<?php
//CONNECT TO THE DATABASE
$con = mysql_connect("XXXXXXX","XXXXXXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("products", $con);
$sql="INSERT INTO products (code, title, desc, img}
VALUES('$_POST
‘,’$_POST[firstname]‘,’$_POST[lastname]‘,’$_POST[age]')";
if (!mysql_query($sql,$con))
{
die('Error: ’ . mysql_error());
}
echo “1 record added”;
mysql_close($con)
?>
I have a form to pass this information which seems to work. When I submit the form I get this error:
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 'desc, img} VALUES('6','fgfg','fgfg','fggf')' at line 1
It would be great if anyone could give me a few tips on this.
Thanks for any help.
N.B - My DB has fields with names that match the variables. I can also update this table via phpmyadmin so I know its working properly.
I changed the field name in the DB to “proddesc” instead and now it works perfectly. Thanks so much for the super fast help. Hopefully one day I’ll be able to help someone here too
I was wrong!! I spotted the “}” symbol and changed it but still get this error…
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 ‘desc, img) VALUES(‘5’,‘asdas’,‘asdasd’,‘asdasd’)’ at line 1
Welcome to Sitepoint! May I inform you that there’s also a MySQL forum where this question would really belong as it is not a PHP error but a MySQL error. Also, if you see anything wrong with your post here, but can’t such as moving it to another subforum in this case, don’t open a new thread but notify the mods by clicking on the orange flag to the left.
Just had a very brief look at it, so I can’t comment on whether there’s more to find, but from what I could see you’re using } instead of ) just after img.
Just one thing for you to do, head over to the introduction forum and introduce yourself properly.