Mysqli PHP - 通过id显示用户信息

时间:2014-12-10 09:22:29

标签: php html mysqli

我需要帮助这个代码bc im new with mysqli

我创建了一个网络来搜索用户并按下按钮以链接http://www.example.com/test.php?id=16 (16是一个示例)但是此代码向我显示了数据库的所有用户和信息以及我只需要id = 16的用户信息。

我已经完成了我搜索用户名的部分和发送给我的按钮test.php?id = 16< - id = 16只是一个例子bc可以是任何数字

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>  
<title>View Records</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>

<h1>View Records</h1>

<p><b>View All</b></p>

<?php
// connect to the database
include('connect-db.php');

// get the records from the database
if ($result = $mysqli->query("SELECT * FROM players ORDER BY id"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table border='1' cellpadding='10'>";

// set table headers
echo "<tr><th>ID</th><th>First Name</th><th>Last Name</th><th></th><th></th></tr>";

while ($row = $result->fetch_object())
{
// set up a row for each record
echo "<tr>";
echo "<td>" . $row->id . "</td>";
echo "<td>" . $row->firstname . "</td>";
echo "<td>" . $row->lastname . "</td>";
echo "<td><a href='records.php?id=" . $row->id . "'>Edit</a></td>";
echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>";
echo "</tr>";
}

echo "</table>";
}
// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}
}
// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}

// close database connection
$mysqli->close();

?>

</body>
</html>

感谢所有人:)

3 个答案:

答案 0 :(得分:1)

你必须通过像

这样的查询传递条件
$id = $_GET['id'];
if ($result = $mysqli->query("SELECT * FROM players WHERE id = ".$id)) {

请注意,此处不需要ORDER BY。为防止SQL注入,您必须mysqli_real_escape_string GET字符串。

答案 1 :(得分:0)

首先在变量中获取id值。

$id_val = $_GET["id"];

并更正您的SQL查询。

"SELECT * FROM players WHERE id ='".$id_val."'"

答案 2 :(得分:0)

在查询中使用where子句,如

if($ result = $ mysqli-&gt; query(&#34; SELECT * FROM players where =&#34;。$ _ GET [&#39; id&#39;]。&#34; ORDER BY id& #34))