I have a problem with my PHP / SQL code. I have tried a lot and Googled but I cannot find which mistake I’ve made with the code.
I could not paste my code here, something went wrong, so I pasted it here
$absentbijwerken = mysql_query("UPDATE hw_absentieregistratie SET '$currentweekandday' = '$aanwezigheid[$i]' WHERE userid = '$userid[$i]'")
or die(mysql_error());
$absentredenbijwerken = mysql_query("UPDATE hw_absentiereden SET '$currentweekandday' = '$reden[$i]' WHERE userid = '$userid[$i]'")
or die(mysql_error());
Both queries get an error.
MySQL error 1064: 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 ‘-Dinsdag = ‘pr’ WHERE userid = ‘1” at line 1″ 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 ‘-Dinsdag =
‘xd’ WHERE userid = ‘1” at line 1
EDIT:
The quotes were the issue, the new code looks like this:
for ( $i = 0; $i < count($userid); $i++){
$absentbijwerken = mysql_query("UPDATE hw_absentieregistratie SET `$currentweekandday` = '$aanwezigheid[$i]' WHERE userid = '$userid[$i]'")
or die(mysql_error());
$absentredenbijwerken = mysql_query("UPDATE hw_absentiereden SET `$currentweekandday` = '$reden[$i]' WHERE userid = '$userid[$i]'")
or die(mysql_error());
Enclose the column names in your queries with backticks:
It seems that the value passed in for
$currentweekanddaycontains a-, which MySQL interprets as an operator, which doesn’t fit in at that place.mysql_Xmethods are deprecated. Switch to PDO or mysqli!