2013-02-03 77 views
0

對於$ item_result-> num_rows,這似乎總是返回1;即使DB中有0行。但是,如果項目存在,它會正確更新該行。我敢肯定,我的語法有些問題,但我很難把這個mysqli包裹起來。Mysqli - > num_rows總是返回1

$item_query = "SELECT COUNT(*) FROM `rel` WHERE `cart_id` = '".$cartId."' && `id_item` = '".$item_id."'"; 
$item_result = $mysqli->query($item_query) or die($mysqli->error.__LINE__); 





if($item_result->num_rows==1) { 


$item_query2 = "SELECT * FROM `rel` WHERE `cart_id` = '".$cartId."' && `id_item` = '".$item_id."'"; 
$item_result2 = $mysqli->query($item_query2) or die($mysqli->error.__LINE__); 

$getOldItems = $item_result2->fetch_array(); 
$oldQty = $getOldItems['amount']; 
$oldNotes = $getOldItems['notes']; 

$newQty = $oldQty + $item_quantity; 
$newNotes = $oldNotes . $item_notes; 


$update_qty = $mysqli->query("UPDATE rel SET amount = '$newQty', notes = '$newNotes' WHERE `cart_id` = '$cartId' && `id_item` = '$item_id'"); 

if(!$update_qty){ 
    printf("Errormessage: %s\n", $mysqli->error); 
} 
    header('Location: ./ordernew.php'); 


} else { 

    $insert_cart_item = $mysqli->query("INSERT INTO rel (`email`, `cart_id`, `id_item`, `amount`, `notes`) VALUES ('$email', '$cartId', '$item_id', '$item_quantity', '$item_notes')"); 

if(!$insert_cart_item) { 
printf("Errormessage: %s\n", $mysqli->error); 
} 
header('Location: ./ordernew.php');  

} 

回答

4

當你做SELECT COUNT(*)時,總會有至少一個結果。即使它的0

您需要讀取查詢的結果,以獲得正確的計數。

+0

謝謝。刪除Count()並且它現在可用!謝謝。 –