URL查询,ID不回显学生姓名

时间:2016-01-22 05:13:55

标签: php mysql

我在一个页面上有一个表格,用户点击一名大学生,然后将他们带到他们的个人资料页面。该个人资料由

链接
<a href="profilePage.php?id=<?php echo $row['ID'] ?>" class="collection-item">

ID是特定学生。在profilePage.php上,它显示了URL上的id,但它没有打印出学生的名字。它只是空白的&#34;学生名称是&#34;

<?php
$idStudent = mysql_real_escape_string($_GET['ID']);
$sql = sprintf("SELECT * FROM student_info WHERE id = %s", $idStudent);
$query = mysql_query($sql);
$student = mysql_fetch_object($query);
?>

<p>Student name is <?php echo $student['student_Name'] ?></p>

我假设我是如何调用id然后从数据库中查询的。我正在使用phpMyAdmin。这就是我连接数据库的方式

<?php
$db = mysql_connect("localhost","server","password");
if (!$db) {
die("Database connection failed miserably: " . mysql_error());
}
$db_select = mysql_select_db("name",$db);
if (!$db_select) {
die("Database selection also failed miserably: " . mysql_error());
}

问题是,即使我在网址中有ID,profilePage.php也没有回显学生姓名。

2 个答案:

答案 0 :(得分:3)

您的网址为id=<?php echo $row['ID'] ?>,并且您使用$_GET['ID']来解决代码$_GET['id']!=$_GET['ID']中的错误。您可以将您的ID设为

$idStudent = mysql_real_escape_string($_GET['id']);// use small id instead id ID

您正在object form

中获取数据
$student = mysql_fetch_object($query);

而不是这个

  <p>Student name is <?php echo $student['student_Name'] ?></p>

使用

<p>Student name is <?php echo $student->student_Name; ?></p>
  

注意: - 不推荐使用mysql而是使用mysqli或PDO

答案 1 :(得分:1)

在您的查询中,您通过id搜索$_GET['ID']可能会出错。 如果你的数据库表字段ID然后使用下面的代码

$sql = sprintf("SELECT * FROM student_info WHERE ID = %s", $idStudent);

;在PHP中放置echo时,您在此处生成查询作为对象,因此您必须像brlow一样回显对象。

<p>Student name is <?php echo $student->firstName; ?></p>

在这里

<a href="profilePage.php?ID=<?php echo $row['ID']; ?>" class="collection-item">