mySQL要求参数为字符串


mySQL expects parameter to be string

好的,所以我基本上有一个表单,用户可以在其中填写信息,在提交表单时,信息会加载到我的SQL数据库中。

有一个"管理员"页面,显示数据库中用户表中的所有用户。

一切都很好,一切都很完美,但我的问题是,我让userId可以点击,这样当我点击userId时,特定的条目就会从数据库中加载。

我无法在屏幕上打印我选择的用户ID。

这是我的管理页面:

<html>
<head>
</head>
<body>
<?php
//Step 1: Set the connection object equal to our database
$conn = mysql_connect("localhost", "root", "") or die (mysql_error());
//Step 2: Set the databse you want to use
mysql_select_db("assignment_3", $conn); 
//Step 3: Write sql that we want to run against the database
$sql = "select id, firstName, lastName, emailAddress from usertable";
//Step 4: Run the sql statement against the database connection object we created $conn
$result = mysql_query($sql, $conn) or die (mysql_error());
//STEP 5: Process the result set $result
print "<table border='1'>";
print "<tr>";
print "<td>id</td>";
print "<td>firstName</td>";
print "<td>lastName</td>";
print "<td>emailAddress</td>";
while ($row = mysql_fetch_assoc($result)) { // this is getting each row that was returned
$clickId = $row['id'];
print "<tr>";   
print "<td><a href ='showUser.php?userId=$clickId'> $clickId </a></td>";
print "<td>$row[firstName]</td>";
print "<td>$row[lastName]</td>";
print "<td>$row[emailAddress]</td>";
}
print "</table>";
?>

</body>
</html>

如果运行此页面,数据库中的所有用户都将加载并显示在一个表中,并且当单击时,他们的所有userId都将成为可单击的链接,并将转到showUser.php页面,该页面显示所选用户的信息。

这是showUser.php页面的代码。我需要使用GET方法从URL中获取userId,然后根据该ID查询DB,以便显示用户信息。

我真的很困,需要一些帮助。

<html>
<head>
</head>
<body>
<?php
$id = filter_input(INPUT_GET, "userId"); 
//Make db connection
$conn=mysql_connect("localhost","root","")or die(mysql_error());
//Step 2: select your db to use
mysql_select_db("assignment_3",$conn);
//Step 3: Write the sql that we want to run against the database
$sql = mysql_query("select id, firstName, lastName, emailAddress, phoneNumber, underGradSchoolId, gradSchoolId, securityQuestionId from usertable where id='$id'");
$result = mysql_query($sql,$conn)or die(mysql_error());
print "<table border='1'>";
print "<tr>";
print "<td>id</td>";
print "<td>firstName</td>";
print "<td>lastName</td>";
print "<td>emailAddress</td>";
print "<tr>";   
print "<td>id</td>";
print "<td>firstName</td>";
print "<td>lastName</td>";
print "<td>emailAddress</td>";
print "<td>phoneNumber</td>";
print "<td>underGradSchoolId</td>";
print "<td>gradSchoolId</td>";
print "<td>securityQuestionId</td>";
print "</table>";

?>

</body>
</html>

您使用mysql_query两次:

$sql = mysql_query("select id, ...");
$result = mysql_query($sql,$conn)or die(mysql_error());

哪个应该(重命名,所以它就像你发布的第一个代码)

$result = mysql_query("select id, ...") or die(mysql_error());
$row = mysql_fetch_assoc($result);

现在你可以打印这样的值

print "<td>".$row['id']."</td>";

还要记住,mysql_*函数是官方不推荐使用的,因此不应在新代码中使用。您可以使用PDO或MySQLi。有关更多信息,请参阅SO上的此答案。

首先验证您是否使用$id获得了期望的值。在第3步之后,打印$sql并确保它以"where id='{selected id}'"结束