php - MySQL 错误 - 每次调用查询时未添加到数据库

标签 php mysql sql syntax-error

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')");

一些注意事项:

  1. 如果您检查代码中的错误,您就会快速轻松地发现这一点。始终寻找并处理错误。对于您的情况,请调用 mysql_error()就可以了。

  2. 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 .

  3. 由于我看不到您在代码中明确处理此问题,因此我必须假设您是 敞开至SQL injections .

关于php - MySQL 错误 - 每次调用查询时未添加到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23316777/

相关文章:

php - 上传到服务器后,Laravel 应用程序无法运行

php - 将 arraylist bean 从 android 传递到 webservice php

java - 将 MySQL JSON 列映射到 hibernate 值类型

android - 向mysql插入数据后无法intent

mysql - 当表2值不存在时,从表1中获取记录并从另一个表中加入它

php - 使用ajax上传表单中的图像提交

php - Mysql 中的同步算术函数

mysql - 选择顺序记录

sql - Web 服务的正确日期时间格式

c# - 使用 LINQ to Entities 多次枚举大型数据集