mysql_query("INSERT INTO accounting(transid, unixtime, username, description, amount, type, time) VALUES('$randomtransid', '0', '$yti', 'BLVD Offer Completion', '$theamount', 'points', '$dateandtime')");
上面的示例代码是我的 MySQL 查询,但每次刷新页面时它都不会添加到 MySQL 数据库中...
我能做什么?
============ 已解决:
添加“mysql_error();”后,
这是错误:
Query failed: Duplicate entry ... for key 'uniqueness'
我通过更改数据库表的结构解决了这个问题。
谢谢
============
最佳答案
表名后需要有一个空格。否则它看起来像一个函数。
mysql_query("INSERT INTO accounting(transid, unixtime, username, description, amount, type, time) VALUES('$randomtransid', '0', '$yti', 'BLVD Offer Completion', '$theamount', 'points', '$dateandtime')");
^^^^^^^
HERE
应该是:
mysql_query("INSERT INTO accounting (transid, unixtime, username, description, amount, type, time) VALUES('$randomtransid', '0', '$yti', 'BLVD Offer Completion', '$theamount', 'points', '$dateandtime')");
一些注意事项:
如果您检查代码中的错误,您就会快速轻松地发现这一点。始终寻找并处理错误。对于您的情况,请调用
mysql_error()
就可以了。You shouldn't use
mysql_*
functions in new code 。它们不再维护and are officially deprecated 。请参阅red box ?了解 prepared statements相反,并使用 PDO ,或MySQLi -this article将帮助您决定哪个。如果您选择 PDO,here is a good tutorial .由于我看不到您在代码中明确处理此问题,因此我必须假设您是 敞开至SQL injections .
关于php - MySQL 错误 - 每次调用查询时未添加到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23316777/