Inserting integer and datetime from PHP to MySQL 5.0 db error

╄→гoц情女王★ 提交于 2019-12-13 01:10:59

问题


I have tried to find out what's wrong.

The table is conditions_loop. One column is condition_id, and the other one is a datetime type.

the code is this

$dt = date("Y-m-d H:i:s");

mysql_query("INSERT INTO conditions_loop (condition_id, date) VALUES ($latest_condition, $dt)") or die(mysql_error());  

$latest_condition is a 1 digit integer.

The error says 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 '13:12:14)' at line 1

I tried everything, but it's something I don't know. Thanks for reading.


回答1:


You should quote the date value:

mysql_query("INSERT INTO `conditions_loop` (`condition_id`, `date`) 
         VALUES ('$latest_condition', '$dt')") or die(mysql_error());  

And while you're at it, quote the table/field names as well, using backticks (`)



来源:https://stackoverflow.com/questions/5452124/inserting-integer-and-datetime-from-php-to-mysql-5-0-db-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!