-1

I am trying to make an order history page for my project. The data is stored in the database table called order_today which holds the ids of the food ordered. But I need to display the name of the food which is stored in another table called menu and combo. I am only getting the result for one entry in the order_today table. the loop is not running. can anybody help? PS: I am a newbie in php.

<?php require 'config.php' ?>

<div class="orderlist">
    <div class="yourlist">
        Your Order History:
        <br>
        <?php
        session_start();
        $sql = "select * from order_today where `user_id`='" . $_SESSION['uid'] ."';";
        $result = mysqli_query($conn, $sql);
        while ($row = $result->fetch_assoc()) {
            $date = explode(' ', $row['time']);
            $combo = unserialize($row['combo_id']);
            $food = unserialize($row['food_id']);

        ?>
            <div class="past-order">
                <div class="Order-hist">
                    <h5><b>Order ID:<?php echo $row['Order_id']; ?></b></h5>
                    <div class="time-date">
                        <h6> Date:<?php echo $date[0]; ?><br>Time:<?php echo $date[1]; ?></h6>
                    </div>
                    <div class="odetails">
                        <?php
                        if (!empty($food['food'])) {
                            foreach ($food['food'] as $item => $itemQuantity) {
                                $sql1 = "select * from menu where `food_id`='" . $item . "';";
                                $result = mysqli_query($conn, $sql1);
                                while ($row1 = $result->fetch_assoc()) {
                                    echo $row1['name'] . ' x' . $itemQuantity . '<br>';
                                }
                            }
                        }
                        if (!empty($combo['combo'])) {
                            foreach ($combo['combo'] as $item => $itemQuantity) {
                                $sql2 = "select * from combo where `combo_id`='" . $item . "';";
                                $result = mysqli_query($conn, $sql2);
                                while ($row2 = $result->fetch_assoc()) {
                                    echo $row2['name'] . '(Combo) x' . $itemQuantity . '<br>';
                                }
                            }
                        }
                        ?>
                    </div>
                    <div class="cost">
                        <h5>Cost: </h5>
                    </div>
                </div>
            </div>
        <?php } ;?>


    </div>
</div>
WOUNDEDStevenJones
  • 5,150
  • 6
  • 41
  • 53
  • On a separate note, you are open to SQL injection (https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) and you should be binding your query parameters instead of concatenating the variables into the SQL query. – WOUNDEDStevenJones Oct 12 '22 at 19:17
  • 3
    Please produce a snippet, complete with what the data looks like - what is expected, and what you are getting. – Stoff Oct 12 '22 at 19:17
  • It looks like you're expecting the first query (`select * from order_today where `user_id`='" . $_SESSION['uid'] ."`) to only return 1 row, correct? So can you verify the value of `$food['food']` before the loop inside of `
    `? Is it an array? Does it have multiple items? Please provide an example of the data returned from the first query to prove to us that this isn't working how you're expecting it to.
    – WOUNDEDStevenJones Oct 12 '22 at 19:21
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Oct 12 '22 at 19:55
  • No it returns multiple rows but it is not displaying the other rows it is displaying only the first row. – Megha Soni Oct 13 '22 at 05:38

2 Answers2

0
  1. session_start(); should be used before any HTML/headers output.
  2. Check $_SESSION['uid'] if it's good by printing the $sql query.
  3. Copy and paste the printed SQL and then run it in MySQL to see what it returns.

Overall, the while loop looks good, the problem could be with your stored data or $_SESSION['uid'] value because of incorrect using of session_start() function.

Valeriu Ciuca
  • 2,084
  • 11
  • 14
0

I solved the query. For all 3 queries I simply used different result variable of the mysqli_query() function and now it works perfectly as needed. PS: Thanks for the response!