dthjnc306679 2015-10-06 10:15
浏览 19

即使语法正确,INSERT / UPDATE也无法正常工作?

PROBLEM:

I've written and been running a PHP script that previously had been working, and today I realize that although it appeared to be functioning correctly, for the past few days no data was being inserted or updated (basically any write queries). This does not trigger any errors, and when the exact same query is copy and pasted into a mysql terminal, it inserts correctly.

CODE

$sql = "REPLACE INTO `history` (`owner`, `user`, `status`, `now_date`)
VALUES ('{$owner['username']}', '{$user}', '1', NOW())";

if (!mysqli_query($db, $sql)) { die('Error: ' . mysqli_error($db)); }

echo "Affected rows: " . mysqli_affected_rows($db);

RESPONSE

Affected rows: 1

So it appears to be working, does not trigger any error, and when the same query is input into a mysql terminal, it works, it's just via PHP that it doesn't. Permissions for the user connecting is fine.

This problem has just happened in the last week, prior to that it was working perfectly. I'm really confused.

Thanks, John.

  • 写回答

1条回答 默认 最新

  • dongliao1949 2015-10-06 10:28
    关注

    You say that a column called id is the UNIQUE key of that table.

    You are not using the id in that query, so how will it be able to know to do an INSERT or UPDATE(DELETE-INSERT) without the id field being there so it can make that decision.

    REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.

    See the manual

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么