gigi
gigi

Reputation: 181

How to use INNER JOIN and FOREIGN KEY between two tables

I got two tables in my databse and I am displaying them in two different php pages.

How can I display the contents of the first table from the page where I am displaying the second table, row by row.

What I want to achive is: each row of the second table have a button that onclick will show (throw a modal-pop-up) the info which are in the first table.

So the button of the row1-table2 will show me only the info of row1-table1 and so on....

I am able to implement the button for each row and the pop-up but I can Only display the info of the entire first table and not of the single row associated.

enter image description here -------------code update

    <div class="container">
          <div class="row text-center">
            <div class="col-md-12 col-sm- hero-feature">
              <div class="thumbnail">  
          <?php
    include("../includes/connection.php");
    if ($link->connect_errno > 0) {
        die('Unable to connect to database [' . $link->connect_error . ']');
    }
if (isset($_POST['update'])) {
$results = $link>query("UPDATE job SET status='$_POST[status]', priority='$_POST[priority]' WHERE id='$_POST[hidden]'");
$results = $link>query("UPDATE **table2** SET status='$_POST[status]' WHERE id='$_POST[hidden]'");}

    $sql = "SELECT * from job";
    if (!$result = $link->query($sql)) {
        die('There was an error running the query [' . $link->error . ']');
    }
    echo "…………./* Get field information for all columns */………… "

    while ($row = $result->fetch_assoc()) {
    echo "<form action='' method=post>";

    echo "<tr class='info'>
    <input type=hidden name=hidden value=" . $row['id'] . ">
    <td>" . $row['id'] . "</td> 
    <td>" . $row['device'] . "</td>
    <td>" . $row['model'] . "</td> 
    <td>" . $row['problem'] . "</td>

    <td><select class='form-control col-sm-10' id='status' name='status'>
    <option value=" . $row['status'] . " >" . $row['status'] . "</option>
                      <option value='new'>New</option>
                      <option value='progress'>Progress</option>
                      <option  value='wait'>Wait</option>
                      <option value='done'>Done</option>
                      <option value='close'>Close</option>
    </select></td>
    <td><select class='form-control col-sm-10' id='priority' name='priority'>
    <option value=" . $row['priority'] . " >" . $row['priority'] . "</option>
                            <option value='high'>High</option>
                            <option value='medium'>Medium</option>
                            <option  value='low'>Low</option>
    </select></td>

    <td>" . $row['status'] . "</td>
    <td>" . $row['priority'] . "</td>

    **<td>   <button type='submit' class='btn btn-primary btn-sm' name='update'>Update</button></td>**

    **<td> <a class='btn btn-primary btn-sm' data-toggle='modal' datatarget='#myModal'>Info</a></td>**
    </tr>";    echo "</form>";}echo "  </tbody>

    </table>";

    ?>
    <div class="container">
      !-- Trigger the modal with a button -->
    <!-- Modal -->
    <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog modal-lg">
    <!-- Modal content-->
    <div class="modal-content">
    <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h4 class="modal-title">Customer Information</h4>

    </div> <div class="modal-body">
    <?php
    include("../includes/connection.php");

    if ($link->connect_errno > 0) {
        die('Unable to connect to database [' . $link->connect_error . ']');
    }
    $sql = "SELECT * from **table2**";
    if (!$result = $link->query($sql)) {
        die('There was an error running the query [' . $link->error . ']');
    }
    echo "
    <table class='table'>
        <thead><tr>";
    /* Get field information for all columns */
    while ($finfo = $result->fetch_field()) {
    echo "<th>" . $finfo->name . "</th>";}echo "
    </tr></thead><tbody>";
    while ($row = $result->fetch_assoc()) {
        echo "<tr class='info'>
        <td>" . $row['id'] . "</td> 
                    <td>" . $row['name'] . "</td>
                    <td>" . $row['mail'] . "</td>
                    <td>" . $row['number'] . "</td>
                    <td>" . $row['price'] . "</td>
                    <td>" . $row['paymenttype'] . "</td>
                    <td>" . $row['faktura'] . "</td>
                    <td>" . $row['date'] . "</td>
        </tr>";}echo "
        </tbody>
        </table>";

    ?> </div>
    <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div></div></div></div></div></div></div>
    <!-- Button trigger modal -->

Upvotes: 0

Views: 245

Answers (1)

Memor-X
Memor-X

Reputation: 2970

From what i am understanding you want a one-to-one/many relationship between the tables (depending if you are planning to reuse customer data).

in table2 have a new field which will contain IDs from table1, this is your foreign key.

What are Foreign Keys?

A foreign key establishes a relationship, or constraint, between two tables.

now i am unsure exactly how you are bring up data when you click "info" but if you are running a separate query when it is clicked then you just pass the table1 ID that's stored in table2 in a WHERE clause like this

SELECT * FROM table1
WHERE custId = ?

where ? is however you generate your queries in PHP (using paramatized queries or generating a a string with PHP variables).

if you need the data from table1 loaded at the same time as table2 then you would use an INNER JOIN

SELECT * FROM table2 AS t2
    INNER JOIN table1 AS t1
        ON t2.custID = t1.custID

this way for every row in table2 it will have a row from table1. this will only work if table1's ID are unique otherwise you can get

NOTE: good practice is not to use SELECT *, it's good when debugging but in proper code you want every field named especially when you do joins incase if 2 tables have field which are named the same

Upvotes: 1

Related Questions