2014-04-29 87 views
0

希望得到SO的php/MySQL專家的建議!我被下面看似簡單的php代碼難住了。PHP MySQL UPDATE語句成功,但列未更新

$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 ??'; 

聲明成功,並說1行受到影響。但image仍然是舊值。當我通過phpMyAdmin輸入相同的sql語句時,更新就起作用了。我嘗試將image更新爲「'和」123「,並且錯誤仍然存​​在,因此它不僅僅是當image = NULL時。

cocoon_result的PRIMARY KEY是idpost_id

編輯:我已經將這段代碼移植到一個新文件中,它的工作原理......它只是不能與原始文件中的其他代碼行一起工作。我已經逐步將代碼的其他部分添加到這個新文件中,它仍然可以工作,但對於我來說,一點一點地添加代碼行直到原始文件被複製爲止會非常瘋狂......另外,我對這種奇怪行爲的原因感到好奇。任何人?

+1

你能在sqlfiddle中重現這個嗎? – Strawberry

+0

不能在sqlfiddle重現這恐怕... – user3585550

+1

然後問題不是你的查詢:-) – Strawberry

回答

3

我認爲你的代碼結構是錯誤的。你的代碼應該是這樣的:

$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 ??'; 
+0

mysqli_affected_rows($ mysqli)的值爲1. – user3585550

+0

我編輯了我的anwer,請再次檢查。 –

+0

那不是它 - $ affected_rows = 1 – user3585550