Search code examples
phpmysqldatabasemysqlicode-injection

In PHP and MySQL, how to view multiple order information in the other page


I've learned a lot from this question, on how to send multiple order information on the database. In my table rows of "order.php" is composed of rows about order information sent by the customers. My code is only for single order only. But I want to view in the other page the multiple orders sent by one customer.

Here is my code for "order.php"

<?php
session_start();
$conn = mysqli_connect('localhost','root','','sampsix');
if(mysqli_connect_errno()){
    echo 'Failed to connect: '.mysqli_connect_error();
}

if(isset($_POST['delete'])){
    $DeleteQuery = "DELETE FROM orders WHERE id='$_POST[hidden]'";
    mysqli_query($conn,$DeleteQuery);
}
if(isset($_POST['view'])){
    header('Location: view_order.php');
}

$query = "SELECT * FROM orders ORDER BY id";
$results = mysqli_query($conn,$query);

echo '<table border="1">';
    echo '<tr>';
        echo '<th>ID</th>';
        echo '<th>Firstame</th>';
        echo '<th>Lastname</th>';
        echo '<th>Email</th>';
        echo '<th>Order Name</th>';
        echo '<th>Order Code</th>';
        echo '<th>Order Qty</th>';
        echo '<th>Sub Total</th>';
    echo '</tr>';

    while($orderData = mysqli_fetch_array($results)){
   echo '<form action="order.php" method="POST">';
    echo '<tr>';
        echo '<td>'.$orderData['id'].'</td>';
        echo '<td>'.$orderData['firstname'].'</td>';
        echo '<td>'.$orderData['lastname'].'</td>';
        echo '<td>'.$orderData['email'].'</td>';
        echo '<td>'.$orderData['ordername'].'</td>';
        echo '<td>'.$orderData['ordercode'].'</td>';
        echo '<td>'.$orderData['orderqty'].'</td>';
        echo '<td>'.$orderData['subtotal'].'</td>';

       echo '<td><input type="hidden" name="hidden" value="'.$orderData['id'].'"></td>';
        echo '<td><input type="submit" name="delete" value="Delete"></td>';
    echo '</form>';
        echo "<td><a href='view_order.php?id=".$orderData['id']."'>View</a></td>";
    echo '</tr>';

}
echo '</table>';
mysqli_close($conn);
?>

And here is my "view_order.php" where in the order information is in there:

<?php
include_once('config.php');

if(isset($_GET['id'])){
    $id = $_GET['id'];

$query = $mysqli->query("SELECT id,firstname,lastname,email,ordername,ordercode,orderqty,subtotal  FROM orders WHERE id='$id'");

if($query){
        while($obj = $query->fetch_object()){
           echo 'ID: '.$obj->id;
           echo 'Firstname: '.$obj->firstname;
           echo 'Lastname: '.$obj->lastname;
           echo 'Email: '.$obj->email;
           echo 'Order Name: '.$obj->ordername;
           echo 'Order Code: '.$obj->ordercode;
           echo 'Order Qty: '.$obj->orderqty;
           echo 'Sub total: '.$obj->subtotal;
        }
   }
}

?>

This code above also execute single order only. I just thinking what if the customers has multiple order and I want to view it all in the other page.


Solution

  • Now you use the id as identifier which refers to just one order. If you want all orders of a customer you should select by the identificator of the customer. In your case i think it is firstname and lastname. You should replace the id with firstname and lastname. You will get something like this:

    <?php
    session_start();
    $conn = mysqli_connect('localhost','root','','sampsix');
    if(mysqli_connect_errno()){
        echo 'Failed to connect: '.mysqli_connect_error();
    }
    
    if(isset($_POST['delete'])){
        $DeleteQuery = "DELETE FROM orders WHERE id='$_POST[hidden]'";
        mysqli_query($conn,$DeleteQuery);
    }
    if(isset($_POST['view'])){
        header('Location: view_order.php');
    }
    
    $query = "SELECT * FROM orders ORDER BY id";
    $results = mysqli_query($conn,$query);
    
    echo '<table border="1">';
        echo '<tr>';
            echo '<th>ID</th>';
            echo '<th>Firstame</th>';
            echo '<th>Lastname</th>';
            echo '<th>Email</th>';
            echo '<th>Order Name</th>';
            echo '<th>Order Code</th>';
            echo '<th>Order Qty</th>';
            echo '<th>Sub Total</th>';
        echo '</tr>';
    
        while($orderData = mysqli_fetch_array($results)){
       echo '<form action="order.php" method="POST">';
        echo '<tr>';
            echo '<td>'.$orderData['id'].'</td>';
            echo '<td>'.$orderData['firstname'].'</td>';
            echo '<td>'.$orderData['lastname'].'</td>';
            echo '<td>'.$orderData['email'].'</td>';
            echo '<td>'.$orderData['ordername'].'</td>';
            echo '<td>'.$orderData['ordercode'].'</td>';
            echo '<td>'.$orderData['orderqty'].'</td>';
            echo '<td>'.$orderData['subtotal'].'</td>';
    
           echo '<td><input type="hidden" name="hidden" value="'.$orderData['id'].'"></td>';
            echo '<td><input type="submit" name="delete" value="Delete"></td>';
        echo '</form>';
            echo "<td><a href='view_order.php?firstname=".$orderData['firstname']."&lastname=".$orderData['lastname']."'>View</a></td>";
        echo '</tr>';
    
    }
    echo '</table>';
    mysqli_close($conn);
    ?>
    

    And the view page:

    <?php
    include_once('config.php');
    
    if(isset($_GET['firstname'])){
        $firstname = $_GET['firstname'];
    if(isset($_GET['lastname'])){
        $lastname = $_GET['lastname'];
    
    $query = $mysqli->query("SELECT id,firstname,lastname,email,ordername,ordercode,orderqty,subtotal  FROM orders WHERE firstname='$firstname' and lastname='$lastname'");
    
    if($query){
            while($obj = $query->fetch_object()){
               echo 'ID: '.$obj->id;
               echo 'Firstname: '.$obj->firstname;
               echo 'Lastname: '.$obj->lastname;
               echo 'Email: '.$obj->email;
               echo 'Order Name: '.$obj->ordername;
               echo 'Order Code: '.$obj->ordercode;
               echo 'Order Qty: '.$obj->orderqty;
               echo 'Sub total: '.$obj->subtotal;
            }
       }
    }
    
    ?>
    

    Note that this structure isn't the best solution. I would store my customers in another table, because what happens if two people have the same first- and lastname?

    In your other question you also create a row for each product in your order table. If you want to do it well you should create another table like order_rules and store your products in that table with an order id. You should normalize your tables. I think this is a good description and tutorial about normalizing tables.

    Please note this code is not safe to use - it contains a number of SQL injection vulnerabilities. It has just been amended into a working state from the code in the original post.