从表A的按钮行收集一行tableB

时间:2016-02-07 01:51:12

标签: php jquery mysql twitter-bootstrap html-table

我在php和bootstrap中使用表格。

  • 我有一个tableA,每行包含一个按钮。
  • 每个按钮的值都等于行号。 (见下文)

    $job_id = $row['id'];
    <td> <a class='btn btn-primary btn-sm'  data-toggle='modal' data-target='#myModal'   name='job_id' value=$job_id  id='job_id'>Info</a></td>
    

我无法做到这一点: - 每个按钮打开一个包含tableB

行的模态弹出窗口

因此,如果我点击tableA第二行的按钮,我想在弹出窗口中显示tableB的row2包含的所有数据,依此类推。

基本上我找不到如何将按钮与另一个表的特定行连接。

  1. 在我的模态中使用此代码,我只能收集整个tableB,但不能收集单行。

    $sql = "SELECT * from tableb WHERE id=job_id ";
    
  2. 在我的模态中使用此代码,我总是收集tableB的第3行所有按钮......

    $sql = "SELECT * from tableb WHERE id=3 ";
    
  3. 在我的模态中使用此代码,我为每个按钮收集了我插入的最后一行

    $sql = "SELECT * from tableb WHERE id='$job_id' "; 
    
  4. (表A和表B用外键连接)
    TableB具有(id,job_id),其job_id = id为tableA
    TableA有(id)

    有什么建议可以帮助我实现我想要的目标吗?

    enter image description here

    更新1 - &gt;文件xxx.php包含加载TableA的脚本和加载Modal的脚本弹出

    加载TableA

    脚本

         <?php
        include("../includes/connection.php");
        if ($link->connect_errno > 0) {
            die('Unable to connect to database [' . $link->connect_error . ']');
        }
        $sql = "SELECT * from TableA";
        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 ($row = $result->fetch_assoc()) {
            $job_id = $row['id'];
            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> <a class='btn btn-primary btn-sm'  data-toggle='modal' data-target='#myModal'   name='job_id' value=[$job_id]  >  Info</a></td>
                    </tr>";
            echo "</form>";
        }    echo "   </tbody>   </table>";  ?>
    
    使用tableB

    加载模式弹出窗口的

    脚本

     <?php
    
    include("../includes/connection.php");
    
    if ($link->connect_errno > 0) {
        die('Unable to connect to database [' . $link->connect_error . ']');
    }
    
    $sql = "SELECT * from TableB WHERE job_id=$job_id ";
    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>";?>
    

2 个答案:

答案 0 :(得分:1)

您可以使用JOIN指令通过一个请求链接两个表。

// For the third button the SQL request will be :
$sql="SELECT * FROM tableA A  INNER JOIN tableb B ON B.job_id=A.id WHERE B.id=3";
// For the fourth button the SQL request will be : 
$sql="SELECT * FROM tableA A  INNER JOIN tableb B ON B.job_id=A.id WHERE B.id=4";

请注意,我在请求中使用了别名。例如,A是表A的别名。因此,A.id是tableA的id列。

请查看此链接以获取有关SQL联接的更多信息:http://www.w3schools.com/sql/sql_join.asp

答案 1 :(得分:1)

要获取tableB中对应于tableA的id的行,您应该写:

$sql = "SELECT * from tableb WHERE job_id = $jobId ";

请注意,我将'WHERE job_id =',而不是'WHERE id ='。

有一个答案建议加入。使用JOIN,您将获得tableA中的行和tableB中的数据