0

I have a foreach MySQL query that I expect to return all results with an identical order ID, yet it yields the same product ID. It knows the correct amount of entries with that particular order ID, as the counter stops after running through all entries with the same order ID.

My foreach statement:

$i = 0;
foreach ($_SESSION["cart_array"] as $per_item)
    {
        $item_id = $per_item['latestid'];
        $fetchorder = mysql_query("SELECT ISBN FROM transactions WHERE Order_ID='$latestid'");
        while ($row = mysql_fetch_array($fetchorder))
            {
                $ISBN = $row["ISBN"];   
            };

        echo "ISBNs in order $latestid are: $ISBN";
        echo "<br>";

        $i++;
    };

$latestid is obtained from a previous query, that correctly results in collecting the most recent order ID.

My database table looks like this:

Table: transactions
Order_ID | ISBN
      25 | 11111111
      25 | 22222222
      25 | 33333333

Yet, my yielded results are:

latestid = 25
ISBNs in order 25 are: 33333333
ISBNs in order 25 are: 33333333
ISBNs in order 25 are: 33333333

(The latestid = 25 is echoed from the previous sql query, just for testing purposes to ensure it works correctly.)

What's causing this to display the same item ID (ISBN) when it should list all from that particular order ID?

Thanks in advance,

Jamie.

12
  • Table: transactions Order_ID | ISBN 25 | 11111111 25 | 22222222 25 | 33333333 see the result, the id is 25 for all, and its being passed in the next query, thus same ans for 3 Commented May 17, 2017 at 5:45
  • What is the output from var_dump($_SESSION);? Commented May 17, 2017 at 5:45
  • Read up on SQL injection. This code is dangerous and needs to be replaced Commented May 17, 2017 at 5:45
  • Don't use the deprecated and insecure mysql_*-functions. They have been deprecated since PHP 5.5 (in 2013) and were completely removed in PHP 7 (in 2015). Use MySQLi or PDO instead. Commented May 17, 2017 at 5:48
  • ISBN = $row["ISBN"]; <- You are overwriting that variable in each iteration so it will only contain the ISBN from the last iteration when you echo it (which you do after the loop). Commented May 17, 2017 at 5:51

2 Answers 2

2

Your echo is not inside the loop so its printing only last assignment to the $ISBN . Place it inside the while loop. your code should be this.

i = 0;
foreach ($_SESSION["cart_array"] as $per_item)
    {
        $item_id = $per_item['latestid'];
        $fetchorder = mysql_query("SELECT ISBN FROM transactions WHERE Order_ID='$latestid'");
        while ($row = mysql_fetch_array($fetchorder))
            {
                $ISBN = $row["ISBN"]; 
                echo "ISBNs in order $latestid are: $ISBN";
                echo "<br>";  
            };
        $i++;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this has partially worked, my yielded results now show all ISBN's associated with that order ID, but now 3 times each. (I'm assuming due to the counter knowing there are 3 entries under that order ID.)
0

Try this

$i = 0;
foreach ($_SESSION["cart_array"] as $per_item)
    {
        $item_id = $per_item['latestid'];
        $fetchorder = mysql_query("SELECT ISBN FROM transactions WHERE Order_ID='$latestid'");
        while ($row = mysql_fetch_array($fetchorder))
            {
                $ISBN = $row["ISBN"];   
             echo "ISBNs in order $latestid are: $ISBN";
              echo "<br>";
            };




        $i++;
    };

1 Comment

A good answer includes an explanation on why the OP should "try this".

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.