Search for PK and show all relating data

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
php-forum Fan User
php-forum Fan User
Posts: 974
Joined: Mon Oct 01, 2012 12:32 pm

Mon Mar 11, 2013 8:14 am

You're sure you want to make all of your order information visible to whomever puts in a number? Personally I think that's a horrible idea, but only damaging to yourself and your business so I'm happy to help. I've made some assumptions here based on the limited info provided. The assumptions are:
1) that you are properly using PDO for your database access, the type of database doesn't matter
2) that you have a database object with a 'getInstance()' method that returns a connection to your database
3)all table and row names are assumptions

Code: Select all


if ($_POST) {//assuming your form posts to this script
    $db = Database::getInstance();
    $custNo = $_POST['custNo'];
    $sql = "SELECT * FROM orders where customer_number = " . $custNo;
    $result = $db->query($sql);
    if ((!$result) || $result->rowCount() == 0) {
        return false; //query error or no records for that custNo
    echo '<table>';
    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
        echo '<tr><td>' . $row['product_number'] . '</td><td>' . $row['product_description'] . '</td><td>' . $row['qty'] . '</td><td>' . $row['order_date'] . '</td></tr>';
    echo '</table>';

Post Reply