0

これは、$item_result->num_rows; に対して常に 1 を返すようです。DBには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(*)常に少なくとも1つの結果があります。その0であっても。

正しいカウントを取得するには、クエリの結果をフェッチする必要があります。

于 2013-02-03T03:29:58.457 に答える