连接两个表 - 逐行关联 [英] Connecting two tables - row by row association

查看:42
本文介绍了连接两个表 - 逐行关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

所以 row1-table2 的按钮将只显示 row1-table1 的信息,依此类推....

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

-------------代码更新

 

<div class="row text-center"><div class="col-md-12 col-sm- hero-feature"><div class="缩略图"><?phpinclude("../includes/connection.php");如果($link->connect_errno>0){die('无法连接到数据库 [' . $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 = "从作业中选择 *";if (!$result = $link->query($sql)) {die('运行查询时出错 [' . $link->error . ']');}echo "…………./* 获取所有列的字段信息 */………… "while ($row = $result->fetch_assoc()) {echo "
";echo "<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'>等待</option><option value='done'>Done</option><option value='close'>关闭</option></选择></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></选择></td><td>" . $row['status'] . "</td><td>" . $row['priority'] . "</td>**<button type='submit' class='btn btn-primary btn-sm' name='update'>Update</button></td>****<a class='btn btn-primary btn-sm' data-toggle='modal' datatarget='#myModal'>Info</a></td>**</tr>"; echo "</form>";}echo " </tbody></table>";?><div class="容器">!-- 用按钮触发模态 --><!-- 模态--><div class="modalfade" id="myModal" role="dialog"><div class="modal-dialog modal-lg"><!-- 模态内容--><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal">&times;</button><h4 class="modal-title">客户信息</h4>

<div class="modal-body"><?phpinclude("../includes/connection.php");如果($link->connect_errno>0){die('无法连接到数据库 [' . $link->connect_error . ']');}$sql = "SELECT * from **table2**";if (!$result = $link->query($sql)) {die('运行查询时出错 [' . $link->error . ']');}回声"

<thead><tr>";/* 获取所有列的字段信息 */while ($finfo = $result->fetch_field()) {echo "<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>";}回声"</tbody></table>";?>

<div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">关闭</button></div></div></div></div></div></div></div><!-- 按钮触发模态-->

解决方案

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

在 table2 中有一个新字段,它将包含 table1 中的 ID,这是您的 外键.

<块引用>

什么是外键?

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

现在我不确定当你点击信息"时你是如何调出数据的;但是如果您在单击时运行单独的查询,那么您只需将存储在 table2 中的 table1 ID 传递到 WHERE 子句中

SELECT * FROM table1哪里 custId = ?

? 然而,您在 PHP 中生成查询(使用参数化查询或生成带有 PHP 变量的字符串).

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

SELECT * FROM table2 AS t2INNER JOIN table1 AS t1开启 t2.custID = t1.custID

这样对于 table2 中的每一行,它都会有 table1 中的一行.这仅在 table1 的 ID 唯一 时才有效,否则您可以获得

注意:好的做法是不要使用 SELECT *,这在调试时很好,但在适当的代码中,您希望每个字段都命名,尤其是在您进行连接时,如果 2 个表具有名称相同的字段

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.

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

解决方案

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

这篇关于连接两个表 - 逐行关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆
".$finfo->名称."</th>";}回声"</tr></thead><tbody>";while ($row = $result->fetch_assoc()) {echo "