drsl90685154 2014-04-26 21:35
浏览 37
已采纳

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

mysql_query("INSERT INTO accounting(transid, unixtime, username, description, amount, type, time) VALUES('$randomtransid', '0', '$yti', 'BLVD Offer Completion', '$theamount', 'points', '$dateandtime')");

The example code above is my MySQL query but it does not get added into the MySQL database every time I refresh the page...

What can I do?

============= SOLVED:

After adding "mysql_error();", this is the error:

Query failed: Duplicate entry ... for key 'uniqueness'

I fixed this by changing the structure of my database table.

Thanks

=============

  • 写回答

1条回答 默认 最新

  • dtpw54085 2014-04-26 21:53
    关注

    You need a space after your table name. Otherwise it looks like a function.

    mysql_query("INSERT INTO accounting(transid, unixtime, username, description, amount, type, time) VALUES('$randomtransid', '0', '$yti', 'BLVD Offer Completion', '$theamount', 'points', '$dateandtime')");
                                    ^^^^^^^ 
                                      HERE
    

    Should be:

    mysql_query("INSERT INTO accounting (transid, unixtime, username, description, amount, type, time) VALUES('$randomtransid', '0', '$yti', 'BLVD Offer Completion', '$theamount', 'points', '$dateandtime')");
    

    A few notes:

    1. If you checked for error in your code you would have caught this quickly and easily. Always look for and handle errors. In your case a call to mysql_error() would have done the trick.

    2. You shouldn't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

    3. Since I can't see you handling this explicitly in your code I must assume you are wide open to SQL injections.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测