简体   繁体   English

PHP MySQL UPDATE语句成功,但列未更新

[英]PHP MySQL UPDATE statement succeeds but column is not updated

Would appreciate the advise of SO's php/MySQL experts! 不胜感激SO的php / MySQL专家的建议! I am stumped by the following seemingly simple piece of php code. 我为以下看似简单的php代码所困扰。

$queryDel = 'UPDATE `cocoon_result` SET `image` = NULL WHERE `id` = "B" AND `post_id` = 183';

$resultDel = mysqli_query($mysqli, $queryDel);
if (!$resultDel)
    $msg .= 'Errormessage: ' . mysqli_error($mysqli) . '<br />';
else if (mysqli_affected_rows($mysqli) == 0)
    $msg .= 'Errormessage: ' . mysqli_error($mysqli) . '<br />';
else
    $msg .= mysqli_affected_rows($mysqli) 'row(s) affected. What the ??';

The statement succeeds and says that 1 row is affected. 语句成功,并说1行受到影响。 However image is still the old value. 但是image仍然是旧值。 When I entered the same sql statement via phpMyAdmin, the update works. 当我通过phpMyAdmin输入相同的sql语句时,更新有效。 I have tried updating image to '' and '123' and the error persists so it's not just when image = NULL. 我尝试将image更新为''和'123',但错误仍然存​​在,因此不仅是image = NULL。

The PRIMARY KEY for table cocoon_result is id and post_id . cocoon_result的主键是idpost_id

Edit: I've transplanted this snippet of code into a new file and it works... it just doesn't work together with the other lines of code in the original file. 编辑:我已经将这段代码移植到了一个新文件中,并且可以正常工作……它根本无法与原始文件中的其他代码行一起使用。 I've incrementally added back the other parts of the code to this new file and it still works but it's going to be quite crazy for me to add lines of code bit by bit till the original file is reproduced... Plus I'm darn curious as to the reason for this strange behaviour. 我已经将代码的其他部分逐步添加回了这个新文件,并且仍然可以正常工作,但是对于我来说,逐行添加代码行直到复制原始文件将是非常疯狂的事情。奇怪的是,这种奇怪行为的原因。 Anyone? 任何人?

I think your code structire is wrong. 我认为您的代码结构错误。 Your code should be like this: 您的代码应如下所示:

$queryDel = 'UPDATE `cocoon_result` SET `image` = NULL WHERE `id` = "B" AND `post_id` = 183';

$resultDel = mysqli_query($mysqli, $queryDel);
$affected_rows = mysqli_affected_rows($mysqli);

if ($affected_rows === -1) //if the query has failed, displaying the error
    $msg .= 'Error deleting result list image: ', mysqli_error($mysqli);
else if (mysqli_affected_rows($mysqli) == 0) //if the query hasn't returned any rows
    $msg .= 'No rows affected<br />';
else
    $msg .= mysqli_affected_rows($mysqli) 'row(s) affected. What the ??';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM