I’m trying to save some inputs from a form into my MySQL database, but I’m receiving this error:
Not saved. 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, remarks) VALUES( ‘a’, ‘b’, ‘c’ at line 1
Here’s my full PHP code:
if (isset($_POST['region'], $_POST['desc'], $_POST['remarks']) && !empty ($_POST['region']) && !empty($_POST['desc']) && !empty($_POST['remarks']) )
{
$region = mysql_escape_string($_POST['region']);
$desc = mysql_escape_string($_POST['desc']);
$remarks = mysql_escape_string($_POST['remarks']);
mysql_query("INSERT INTO register (region, desc, remarks) VALUES(
'$region',
'$desc',
'$remarks') ") or die('Not saved. ' . mysql_error());
}
Can you please tell me what I’m doing wrong? I’ve done this before but I can’t seem to find the error in this case. 🙁
As others already pointed out,
descis SQL keyword, but you do NOT have to change your column name to make your query work. You probably consider this, but definitely it is not mandatory. The trick is that (and this is good habit to do all the time) you should always quote column names using backticks ” ` ” to not make SQL engine confused. So this would throw an error:but this is perfectly fine: