0

I have two data tables in a MySQL database as follows.

Customer Table data fields are Id, Name, Contact_Number, status, Customer_Id Items Table data fields are Id, Item_Name, Price. The foreign key is Customer_Id in the item table.

I created a form to save items. if I fill the form item table Customer_id had been saved as I was given.

**Then I need to show correctly selected item and that saved customer id's data in a web page table **

The page table fields are | Item Id | Customer Name | Contact No | Price |

I coded as follows.

<?php 

    require_once("../inc/conn.php");
    $query = " select customer.*,item.* from customer,items WHERE status='1' ORDER BY id DESC LIMIT 5";
    $result = mysqli_query($con,$query);

?>

                <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                  <thead>
                    <tr>
                                <th> ID </th>
                                <th> Customer Name</th>
                                <th> Contact No</th>
                                <th> Price </th>
                    </tr>
                  </thead>
                  <tbody>
                    <tr>
                      <?php 
                                    while($row=mysqli_fetch_assoc($result))
                                    {
                                        $id = $row['id'];
                                        $c_name = $row['Name'];
                                        $contact_no1 = $row['Contact_Number'];
                                        $contact_no2 = $row['Price'];
                                    <tr>
                                        <td><?php echo $id ?></td>
                                        <td><?php echo $name?></td>                                    
                                        <td><?php echo $Contact_Number?></td>
                                        <td><?php echo $Price?></td>
                            ?>

  • What's the problem with the given code? Anything not working? If yes: what have you tried to make it work? – Nico Haase Feb 14 '20 at 16:07
  • I think that query error. it is not working. how to get customer table data tor show.? – Udayanga Amarasinghe Feb 14 '20 at 16:11
  • 1
    If that is your full/actual code, you have a parse error and undefined variables. Enable error reporting. – Funk Forty Niner Feb 14 '20 at 16:14
  • I had been cut some things for that question to be easy. – Udayanga Amarasinghe Feb 14 '20 at 16:19
  • it didn't show customer table data. Shown only item table data. – Udayanga Amarasinghe Feb 14 '20 at 16:20
  • You probably want to join the data so try this: `SELECT c.Id, c.Name, c.Contact_Number, i.Price FROM Customer c LEFT JOIN Items i ON c.Customer_Id = i.Customer_Id WHERE c.status = '1' ORDER BY c.Id DESC` – Patrick Janser Feb 14 '20 at 16:23
  • Contrast `$c_name = $row['Name'];` with `echo $name` and `$contact_no1 = $row['Contact_Number'];` with `echo $Contact_Number`. – Patrick Q Feb 14 '20 at 16:25
  • 1
    If you want some help from the community, try to be clear in your description of your problem. Have you got 2 or 3 tables in the end? Use MarkDown to show them clearly and then get a SQL query to work properly in your phpMyAdmin or other client. Once this is ok then you can code your PHP. – Patrick Janser Feb 14 '20 at 16:27

0 Answers0