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.

Jamjar91
  • 25
  • 6
  • 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 – Exprator May 17 '17 at 05:45
  • What is the output from `var_dump($_SESSION);`? – Gabriel Heming May 17 '17 at 05:45
  • Read up on SQL injection. This code is dangerous and needs to be replaced – Nathan Dawson May 17 '17 at 05: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. – M. Eriksson May 17 '17 at 05: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). – M. Eriksson May 17 '17 at 05:51
  • @GabrielHeming I attempted to add a "print_r var_dump($_SESSION);" It causes the website to return a HTTP 500 error. Is there another method for checking this, it's new to me :) – Jamjar91 May 17 '17 at 05:54
  • @NathanDawson I totally understand that this method is outdated, I'm currently a student doing a project, this is the method we are taught in class, hence why I'm using PHP 5.6.30 over the most current version. This project will never be used outside of education, so I don't have to worry about it here, I appreciate the concern though. Thanks. After this project, I will be updating and using PDO. – Jamjar91 May 17 '17 at 05:55
  • @MagnusEriksson I'm sorry, it's the method I was taught for this project, I understand the dangers behind it. This will never be used outside of this educational project, and I will be updating to PHP 7 after. (Currently using 5.6.30, as told to do so in class.) – Jamjar91 May 17 '17 at 05:56
  • If you're using PHP 5.6, you should still not use `mysql_*` since it was already _deprecated_ back in PHP 5.5. If you're just learning PHP, you should learn it correctly, using non deprecated features. Learning and using deprecated features doesn't make sense, since you just need to relearn it all again. – M. Eriksson May 17 '17 at 05:57
  • @MagnusEriksson Trust me, after this project, I will be updating both my PHP version and methods. At the start when I had no knowledge what-so-ever, the lecturers had taught us these SQL queries and explained that we should be using 5.6 for consistency throughout their teachings and available resources from the college. – Jamjar91 May 17 '17 at 06:00
  • You should do either `print_r($_SESSION)` or `var_dump($_SESSION)`. Not `print_r var_dump($_SESSION)` (which is invalid syntax). If you get a 500 error, you should turn on error reporting and display errors to see the actual error message. Here's a post on how to do that: http://stackoverflow.com/questions/5438060/showing-all-errors-and-warnings (that will probably also show you a deprecated warning). – M. Eriksson May 17 '17 at 06:01
  • @MagnusEriksson Thanks, I've got that working now, I can see what's being stored in session, this is quite handy. :) – Jamjar91 May 17 '17 at 06:15

2 Answers2

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++;
    }
Mofiqul Islam
  • 196
  • 1
  • 15
  • 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.) – Jamjar91 May 17 '17 at 06:09
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++;
    };