1

I am currenlty working on a shopping cart. I have run into an issue where fetch_array is not pulling results from items in a mysql DB. I need this to pull a current quantity and the user notes on the item. It then adds the new value to the old value and combines the notes. Currently, only $newQty & $newNotes are returning a value which is from a form on the page before. Any ideas?

$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) {

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

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

    print("Old QTY: $oldQty, Old Notes: $oldNotes, New QTY: $newQty, New Notes:         $newNotes");
   }
}

1 Answer 1

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_query = "SELECT * FROM `rel` WHERE `cart_id` = '" .$cartId. "' && `id_item` = '" .$item_id. "'";
    $item_result2 = $mysqli->query($item_query) or die($mysqli->error.__LINE__);

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

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


        print("Old QTY: $oldQty, Old Notes: $oldNotes, New QTY: $newQty, New Notes: $newNotes");
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I'll try but it's is counting the num_rows correctly. Thanks!
Oh, I see, you need another query
Many thanks! I tried before but I must have spelled something wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.