如何从2个SQL表中获取数据并将其放在单个HTML表中?

时间:2016-06-04 07:22:32

标签: php html mysql

PHP就在这里:

 <?php
    $sth = $conn->prepare('SELECT employee.name, employee.type, employee.rate, work.overtime, work.leaves, work.ticket FROM employee, work');
   $sth->execute();
     $data = $sth->fetchAll();   
    foreach ($data as $row ){
        if($row['name']!=""){
    ?>

HTML:

<tr>
      <td>
        <input type="text" placeholder="Name" value="<?php echo $row['name']?>"/>
      </td>
      <td>
        <input type="text" placeholder="Type" value="<?php echo $row['type']?>"/>
      </td>
      <td>
        <input type="text" placeholder="Rate" value="<?php echo $row['rate']?>"/>
      </td>
      <td>
        <input type="text" placeholder="OT" value="<?php echo $row['overtime']?>"/>
      </td>
      <td>
        <input type="text" placeholder="Leaves" value="<?php echo $row['leaves']?>"/>
      </td>
      <td>
        <input type="text" placeholder="Total" value="<?php echo $row['ticket']?>"/>
      </td>
    </tr>

表格快照: enter image description here

另一个: enter image description here

名称在两者之间很常见。

如何显示数据?

2 个答案:

答案 0 :(得分:3)

您正在寻找的术语是JOIN。阅读MySQL Official Doc

的手册

答案 1 :(得分:0)

a => (a < 10) ? 'valid' : 'invalid'

然后您的HTML代码将如下所示(确保您在<?php $sth = $conn->prepare('SELECT employee.name, employee.type, employee.rate, work.overtime, work.leaves, work.ticket FROM employee left join work on employee.name=work.name'); $sth->execute(); $data = $sth->fetchAll(); foreach ($data as $row ){ if($row['name']!=""){ ?> 代码下使用<tr>。)

<table>

我在这里使用左连接,因为您可能需要所有员工姓名。如果你只想要这两个表之间的公共记录,那么只需使用内连接而不是左连接。