I am getting this 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 '(update_id
, update_when
, update_title
, update_text
, update_who
) VALUES' at line 1
I have searched this site and many others for resolution and I can not seem to find it. I have tried just about every suggestion that I can find, now time to see if I am missing something obvious (for the past 3 days). Please take a look at my code and see what is wrong. I have tested the code with random echo = "1"; throughout my code to find where it breaks, and I will post from there on. Many thanks in advance and if you need more that what I post, feel free to ask.
$update = mysql_query("INSERT INTO `cbhof_news` SET (`update_id`, `update_when`, `update_title`, `update_text`, `update_who`) VALUES (NULL, `$when`, `$title`, `$text`, `$who`") or die(mysql_error());
echo "1"; //this one is not showing yet, so I know its borked here.
if (mysql_query($update)){
//header('location: news.php');
echo "Posted! <br /><br />$title - by: $who<br />$text";
}else{
die('We are sorry, this entry could not be created. <br />Error: ' . mysql_errno() . ': ' . mysql_error());
}
}
Thanks again!!
EDIT: Okay, my code now reads (thanks to Rakesh Sharma):
$update = mysql_query("INSERT INTO cbhof_news (`update_id`, `update_when`, `update_title`, `update_text`, `update_who`) VALUES ('', '$when', '$title', '$text', '$who')") or die(mysql_error());
if ($update == TRUE){
//header('location: news.php');
echo "Posted! <br /><br />$title - by: $who<br />$text";
}else{
die('We are sorry, this entry could not be created. <br />Error: ' . mysql_errno() . ': ' . mysql_error());
}
}
?>
But now I am seeing multiple entries being made in the database. First it just inserted one, the second time two, then four, then eight....
Any thoughts?
Dont use SET
in the query if you are inserting new row
$update = mysql_query("INSERT INTO `cbhof_news` (`update_id`, `update_when`, `update_title`, `update_text`, `update_who`) VALUES (NULL, '$when', '$title', '$text', '$who'") or die(mysql_error());
try to use single quotes on query variables and remove SET
keyword Also close your query correctly
$update = mysql_query("INSERT INTO `cbhof_news` (`update_id`, `update_when`, `update_title`, `update_text`, `update_who`) VALUES ('', '$when', '$title', '$text', '$who')") or die(mysql_error());
For more insert query :- http://dev.mysql.com/doc/refman/5.6/en/insert.html
using SET
you need query like:-
INSERT INTO table SET col_name = value,col_name = value,col_name = value,...
Just remove the SET keyword.
$update = mysql_query("INSERT INTO `cbhof_news` (`update_id`, `update_when`, `update_title`, `update_text`, `update_who`) VALUES (NULL, `$when`, `$title`, `$text`, `$who`") or die(mysql_error());
And make sure your values are correctly escaped to prevent sql injection.
INSERT INTO cbhof_news
(update_id
, update_when
, update_title
, update_text
, update_who
) VALUES (NULL, '$when', '$title', '$text', '$who')")
3 changes:
1_ Removed Set since you are inserting and not updating.
2_ put ' instead of ` with the values.
3_ At the end you close " before you are closing the brackets.
Find edited code above, the code should work when you replace it.