Hi

I'm trying to make a system for someone I know and I have a customers page with a link to view orders and created another page to display the orders made for that specific customer name, I have got some php code on the page but it looks like it's only getting the first record of the customer name instead of all the records for the customer name

The code on the customer_orders.php page is below

<?php

                                /*error_reporting(E_ALL);
                                ini_set('display_errors', 1);*/

                                $CustomerName = $_GET['customername'];
                                //echo htmlentities($CustomerName);

                                $sql = "SELECT 
                                                    tblorders.InvoiceNumber,
                                                    tblorders.LorryName,
                                                    tblorders.CustomerName,
                                                    tblorders.DeliveryDate,
                                                    tblorders.PaymentMode,
                                                    tblorders.DeliveryMethod,
                                                    GROUP_CONCAT(CONCAT(tblorders.Quantity, ' of ', tblproducts.ProductName) SEPARATOR ', ') AS productnames, 
                                                    tblorders.InvoiceGenDate
                                                FROM tblorders 
                                                JOIN tblproducts ON tblproducts.id = tblorders.ProductId
                                                JOIN tblcustomers ON tblorders.CustomerName = tblcustomers.customername
                                                WHERE tblorders.CustomerName = :CustomerName";

                                $statement = $dbh->prepare($sql);
                                $statement->bindValue(':CustomerName', $CustomerName);
                                $result = $statement->execute();

                                if(!$result)
                                {
                                    //Query failed
    echo "Query failed";
    //Add debugging code
                                }
                                elseif(!$statement->rowCount())
{
    //No results returned
    echo "No user found for user " . htmlentities($CustomerName);
   //Add debugging code
}
                                else
{
    //A record was returned, display results
    $row = $statement->fetch(PDO::FETCH_ASSOC);

                                    ?>

                                <tbody>
                                    <tr>
                                        <td><?php echo $row['InvoiceNumber']; ?></td>
                                        <td><?php echo $row['LorryName'] ;?></td>
                                        <td><?php echo htmlentities(date("d-m-Y", strtotime($row['DeliveryDate'])));?></td>
                                        <td><?php echo $row['PaymentMode'];?></td>
                                        <td><?php echo $row['DeliveryMethod'];?></td>
                                        <td><?php echo htmlentities(date("d-m-Y", strtotime($row['InvoiceGenDate'])));?></td>
                                        <td><?php echo $row['productnames'];?></td>
                                        <td class="project-actions text-right">
                                        <a class="btn btn-primary btn-sm" href="#">View Invoice</a></td>
                                    </tr>
                            </tbody>

                            <?php

    //echo "Start Date: {$row['CustomerName']}<br/>\n";
}

$statement->closeCursor();
                                ?>

I'm not sure where I have gone wrong, I got the code from online and trying to do this code myself, could anyone take a look please and see what I have done wrong

I don't work with PHP although I've done a fair bit of SQL and SQL selects return recordsets that you have to step through to get all of the returned records so I'm thinking you'll need to process the records in a loop. Your comment

/A record was returned, display results

implies that you are expecting only one record. Perhaps try the select in a SQL console to see if you are actually getting back what you expect. What database engine are you using?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.