如何将外键#换到其他表?

时间:2016-01-08 07:08:53

标签: php

我在获取id(其他表中的外键)时遇到麻烦

data struct sample

这是来自student_log.php的代码

<tr class="record" style="text-align:center;">
<td align="center"><a href="get_idno.php?idno=<?php echo $row['idno']; ?>" data-toggle="modal"><?php echo $row['idno']; ?></a></td>
</tr>

这是我的get_idno.php

<?php

$YearNow=Date('Y');
include('../connection/connect.php');
    $idno=$_GET['idno'];
    $result = $db->prepare("SELECT * FROM studentvotes,student,candidates where idno = '$idno' AND candidates.idno = student.idno AND student.idno = studentvotes.idno AND syearid = $YearNow ");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>

<tr class="record" style="text-align:center;">
<td align="center" ><?php echo $row['idno']; ?></td>
<td align="center" ><?php echo $row['candid']; ?></td>
<td align="center" ><?php echo $row['lastname']; ?></td>
<td align="center" ><?php echo $row['firstname']; ?></td>
  </tr>
    <?php
            }
                ?>

我认为是因为$idno=$_GET['idno'];?请帮忙

1 个答案:

答案 0 :(得分:0)

查询应该是:

$stmt = $db->prepare("
    SELECT c.idno, c.candid, s.firstname, s.lastname
    FROM studentvotes AS sv
    JOIN candidates AS c ON sv.candid = c.candid
    JOIN student AS s ON c.idno = s.idno
    WHERE sv.syearid = :year
    AND sv.idno = :idno");
$stmt->bindParam(':year', $YearNow);
$stmt->bindParam(':idno', $idno);
$stmt->execute();

我不得不猜测您希望在结果中显示哪个idno以及要从idno参数中匹配哪个$_GET