在PHP的where子句中使用跨度文本

时间:2019-01-26 15:31:38

标签: javascript php html mysql

enter image description here

此div充当按钮。每当我单击一个时,页面就会转到页面名称“ teacherinfo.php”。 “样本”文本用作教师的姓名。问题是,当我单击“ Sample2”时,示例文本显示在“ teacherinfo.php”页面上。

enter image description here

以下是转到“ teacherinfo.php”的脚本:

<?php
  while ($row = mysqli_fetch_array($query)) {
    echo '<div class="announcementSlider" id="click">
            <img src="pictures/blank photo.png" class="teacherpic"><br>
            <span name="LastName">'.$row['LastName'].'</span><br>
            <span>'.$row['Grade'].' - </span>
            <span>'.$row['Section'].'</span>
          </div>';
  }
?>

<script type="text/javascript">
  var a = document.getElementsByClassName("announcementSlider");
  for (i=0; i<a.length; i++) {
    a[i].onclick = function () {
      location.href = "teacherinfo.php";
    };
  }
</script>

在“ teacherinfo.php”中显示文本的代码:

<div class="leftForm hide-on-med-and-down">
    <img src="pictures/default-avatar-250x250.png">
    <p class="name"><?php echo $name3; ?></p>
    <p class="section"><?php echo $grade3; ?>-<?php echo $section; ?></p>
  </div>

用于检索数据库中数据的代码:

$sql3 = mysqli_query($db,"select * from teacherinfo");
$row3 = mysqli_fetch_array($sql3,MYSQLI_ASSOC);
$name3 = $row3['LastName'];
$grade3 = $row3['Grade'];
$section3 = $row3['Section'];

我想使用“'。$ row ['LastName']。'”作为mysqli_query($ db,“ select * from Teacherinfo”)中的where子句。我怎样才能做到这一点?还是有解决此问题的另一种方法?

1 个答案:

答案 0 :(得分:1)

首先,我不明白为什么您可以使用简单的a标签和正确的href标签来使用javascript:

<?php
  while ($row = mysqli_fetch_array($query)) {
    echo '<a class="announcementSlider" href="teacherinfo.php?id=' . $row['id'] . '">
            <img src="pictures/blank photo.png" class="teacherpic"><br>
            <span name="LastName">'.$row['LastName'].'</span><br>
            <span>'.$row['Grade'].' - </span>
            <span>'.$row['Section'].'</span>
          </a>';
  }
?>

在这里,我想您的mysql表中的每个老师都有一个唯一的id字段。

teacherinfo.php上,您以id的身份访问$_GET['id']并创建一个查询,例如(这里我跳过安全性部分,但是 $_GET['id']可以伪造因此其中包含一些不安全的数据,您必须对其进行检查):

sql3 = mysqli_query($db,"select * from teacherinfo where id = " . $_GET['id']);
$row3 = mysqli_fetch_array($sql3,MYSQLI_ASSOC);
$name3 = $row3['LastName'];
$grade3 = $row3['Grade'];
$section3 = $row3['Section'];