连接两个表 - 逐行关联

时间:2015-11-01 23:27:38

标签: php mysql

我在数据库中有两个表,我在两个不同的php页面中显示它们。

如何从显示第二个表的页面逐行显示第一个表的内容。

我想要的是:第二个表的每一行都有一个按钮,onclick将显示(抛出模态弹出)第一个表中的信息。

所以row1-table2的按钮只显示row1-table1的信息等等......

我能够为每一行和弹出窗口实现按钮,但我只能显示整个第一个表的信息而不是相关的单行信息。

enter image description here -------------代码更新

    <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 -->

1 个答案:

答案 0 :(得分:1)

根据我的理解,您希望表之间存在一对一/多关系(取决于您是否计划重用客户数据)。

table2中的

有一个新字段,其中包含来自table1的ID,这是您的foreign key

  

什么是外键?

     

外键在两个表之间建立关系或约束。

现在我不确定当你点击“info”时你是如何调出数据的,但如果你在单击时运行一个单独的查询,那么你只需要将一个存储在table2中的table1 ID传递给WHERE子句,就像这样< / p>

SELECT * FROM table1
WHERE custId = ?

但是?在PHP中生成查询(使用参数化查询或使用PHP变量生成字符串)。

如果您需要table2中的数据与table2同时加载,那么您将使用INNER JOIN

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

这种方式对于table2中的每一行,它都会有一行来自table1。这只有在table1的ID 唯一时才有效,否则你可以获得

注意:良好的做法是不使用SELECT *,它在调试时很好,但是在正确的代码中,如果2个表具有相同的字段,则需要每个字段命名,特别是当您进行连接时

相关问题