Php CRUD:在单个while循环中显示来自2个相关表的数据

时间:2017-05-06 17:16:25

标签: php mysql crud

我已经制作了一个CRUD应用程序来收集有关患者的信息。

数据库中有两个表:用户 medical_records 。 "用户"表格包含医疗个人广告医疗记录,换句话说,数据到 medical_records 表。

用户ID(uid)是医疗记录表中的外键。我想在 medical_records 表中显示军医的全名。

enter image description here

为此,我首先编写了这个查询:

$sql = "SELECT `first_name`, `last_name` FROM users WHERE id =`" . $row['uid'] . "`";

,结果是所需的结果:

SELECT `first_name`, `last_name` FROM users WHERE id =`4;`

但回显全名echo $row['first_name'] . $row['last_name'];会导致错误:Undefined index: first_name in path\to\medical_records_all_table.php

我试图从单个while循环中的两个表中提取数据的整个上下文是:

<table id="medical_records" class="table table-bordered table-striped" width="100%">
  <thead>
    <tr>
      <th>Nume complet</th>
      <th>Judet</th>
      <th>Data internarii</th>
      <th>Ora internarii</th>
      <th>Data operatiei</th>
      <th>Ora operatiei</th>
      <th>Fisa adaugata de</th>
      <th>Actiuni</th>
    </tr>
  </thead>
  <tbody>
    <?php
      $sql = "SELECT `mid`, `uid`, `nume_complet`, `judet`, `data_internarii_mamei`, `ora_internarii_mamei`, `data_operatiei`, `ora_operatiei` FROM medical_records";

      $result = mysqli_query($con, $sql);
      if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
          $mid = $row['mid'];
          ?>
    <tr id="<?php echo $mid; ?>">
      <td>
        <a href="view_record.php?mid=<?php echo $mid; ?>">
        <?php echo ucwords($row['nume_complet']); ?>
        </a>
      </td>
      <td><?php echo $row['judet']; ?></td>
      <td><?php echo $row['data_internarii_mamei']; ?></td>
      <td><?php echo $row['ora_internarii_mamei']; ?></td>
      <td><?php echo $row['data_operatiei']; ?></td>
      <td><?php echo $row['ora_operatiei']; ?></td>
      <td>
        <?php
          echo $sql = "SELECT `first_name`, `last_name` FROM users WHERE id =`" . $row['uid'] . ";`";
          echo $row['first_name'] . $row['last_name'];
        ?>
      </td>
      <td class="actions">
        <ul class="list-inline text-center">
          <li>
            <a title="Vezi fisa" href="view_record.php?mid=<?php echo $mid; ?>"><span class="glyphicon glyphicon-eye-open"></span></a>
          </li>
          <li>
            <a title="Editeaza" href="edit_record.php?mid=<?php echo $mid; ?>"><span class="glyphicon glyphicon-edit"></span></a>
          </li>
          <li>
            <a title="Sterge" class="delete-icn" href="#" data-mid="<?php echo $mid; ?>"><span class="glyphicon glyphicon-trash"></span></a>
          </li>
        </ul>
      </td>
    </tr>
    <?php } mysqli_free_result($result); } ?>
  </tbody>
</table>

我错过了什么?

1 个答案:

答案 0 :(得分:1)

尝试

$sql = "SELECT `first_name`, `last_name` FROM users WHERE id =`" . $row['uid'] . ";`";
$result = mysqli_query($con, $sql);
while ($row = mysqli_fetch_assoc($result)) {
 echo $row['first_name'] . $row['last_name'];
}

而不是

echo $sql = "SELECT `first_name`, `last_name` FROM users WHERE id =`" . $row['uid'] . ";`";
 echo $row['first_name'] . $row['last_name'];

最好的方法是在第一个sql中加入user和medical_records表,如下所示。

$sql = "SELECT m.mid, m.uid, m.nume_complet, m.judet, m.data_internarii_mamei, m.ora_internarii_mamei, m.data_operatiei, m.ora_operatiei, u.first_name, u.last_name FROM medical_records m join users u on m.uid = u.id";

以下不需要

echo $sql = "SELECT `first_name`, `last_name` FROM users WHERE id =`" . $row['uid'] . ";`";