[英]How do I display specific rows from a SQL database using ID as argument
我有php生成的表,显示数据如下:
ID Name
1 xxxx
2 xxxx
我希望能够单击ID号,并在单独的页面上显示与该ID相关的信息
我到目前为止:
table.php
include("connection.php");
$con=mysql_select_db('fm', $con);
$query = "SELECT * FROM table ;
$result = mysql_query($query);
echo "<table>
<tr>
<th>ID</th>
<th>Location</th>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr><td><a href='send.php?=" . $row['id'] . "'>" . $row['id'] . "</a></td><td>" . $row['location'] . "</td></tr>";
}
echo "</table>";
mysql_close();
?>
info.php
include ("connection.php");
$con=mysql_select_db('fm', $con);
$id=$_GET['id'];
$query = "SELECT * FROM table WHERE id=". $id;
$result = mysql_query($query) or die (mysql_error());
echo "<table>
<tr>
<th>ID</th>
<th>Property</th>
<th>Location</th>
<tr>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['property'] . "</td></td>" . $row['location'] . "</td></tr>";
var_dump($row);
}
echo "</table>";
$result = mysql_query($sql);
mysql_close($con);
任何输入将不胜感激
您在这里有一个错误:
echo "<tr><td><a href='info.php?id=" . $row['id'] . "</td><td>" . $row['location'] . "</td></tr>";
您需要像这样结束<a>
元素:
echo "<tr><td><a href='info.php?id=".$row['id']."'>".$row['id']."</a></td><td>" . $row['location'] . "</td></tr>";
另外,您应该在info.php中使用IF语句,因为如果您像这样访问它:
info.php // without ?id=
您将拥有未定义的变量$ id。 当您在select中使用变量之前不对其进行处理时,它对mysql注入很不利。
您在此行的info.php中出错了:
$result = mysql_query($query, $con);
它应该看起来像这样:
$result = mysql_query($query);
您在table.php中正确了。
我会这样改变echo命令
echo "<tr><td>{$row['id']}</td><td>{$row['property']}</td></td>{$row['location']}</td></tr>";
双引号将照顾变量的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.