0

这似乎总是为 $item_result->num_rows 返回 1; 即使数据库中有 0 行。但是,如果一个项目存在,它会正确更新该行。我确定我的语法有问题,但我很难理解这个 mysqli。

$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

1 回答 1

4

当你这样做SELECT COUNT(*)时,总会有至少一个结果。哪怕是0。

您将需要获取查询结果以获得正确的计数。

于 2013-02-03T03:29:58.457 回答