-1

Can anybody find me where is the error exactly as I'm getting really mad

This is my code:

$query = 'INSERT INTO mk_pay_wages (u_id, year_month, wage) VALUES (14, "2021-06", 900)';

echo $query . '<br>';

$mysqli->query($query) or die ($mysqli->error);

And this is the mySQL table:

enter image description here

And finally this is what it outputs:

INSERT INTO mk_pay_wages (u_id, year_month, wage) VALUES (14, "2021-06", 900)

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 'year_month, wage) VALUES (14, "2021-06", 900)' at line 1

Dharman
  • 30,962
  • 25
  • 85
  • 135
medk
  • 9,233
  • 18
  • 57
  • 79
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman May 30 '21 at 09:49

1 Answers1

2

YEAR_MONTH is a reserved keyword in MySQL:

https://dev.mysql.com/doc/refman/5.7/en/keywords.html#keywords-5-7-detailed-Y

You can use a reserved keyword as an identifier only if you enclose it in back-ticks:

INSERT INTO mk_pay_wages (u_id, `year_month`, wage) ...
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • Thanks a lot! you saved my day (my night) I'm dealing with this at midnight in my local time – medk May 29 '21 at 22:32
  • But why phpMyAdmin did not tell me this the time of the creation of the table! – medk May 29 '21 at 22:35
  • Did you put the column in back-ticks when you created the table? – Bill Karwin May 29 '21 at 23:07
  • no I was created simply with phpMyAdmin and I wrote them without any back-ticks – medk May 30 '21 at 00:36
  • I don't use phpmyadmin, but I would guess phpmyadmin added the back-ticks for you. But regardless, there's no way MySQL Server would accept `year_month` as a column name without being delimited with back-ticks. – Bill Karwin May 30 '21 at 03:25