我可以使用下面的代码显示表中的内容,但正如您在代码中看到的那样,我将行链接到一个新页面,并在该页面上尝试显示同一个表中的其余行。
我是说,我的桌子上有cols ID, photo, Firstname, Lastname, Age, StreetAdd, PhoneNum, EmailAdd。我只在第一页显示行photo, Firstname, Lastname。
因此,我尝试做的是,当用户单击我在数据库中显示的名字时,他将被重定向到新页面,并查看其余信息。我该怎么做呢?
这是PHP页面,其中显示了这三个cols。我可以在新页面上显示其余的cols,但它显示的是行中的所有信息。我想要显示每个用户的个人信息,而不是整个列表。一个可能的例子是eBay。搜索项目时,只有单击图片或标题,才能看到完整的描述。
<?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("simple_login", $con);
$result = mysql_query("SELECT * FROM test ");
echo "<table align='center' bgcolor='#F9F0F0' border='0' cellspacing='0'>
<tr>
<th><font color='red'>Firstname</font></th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td><a href='send.php'><img src='".$row['photo']."' \" width=\"150px\" height=\"150px\" /></a><br><br><br>";
echo "<a href='send.php'><td align='center' style='vertical-align:text-top' width='200px'>" . $row['Firstname'] . "</td>";
echo "<td align='center' style='vertical-align:text-top' width='200px'>" . $row['Lastname'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>发布于 2013-01-06 14:13:16
要显示每个用户的单独信息,可以在查询中使用where close,如
SELECT * FROM test WHERE user = bla警告
您的代码容易受到sql injection的攻击,您需要避开所有get和post,更好的方法是使用Prepared statement
好读
便笺
PHP整个ext/mysql扩展提供了所有以mysql_前缀命名的函数,它是officially deprecated as of PHP v5.5.0,将来将被删除。因此请使用PDO或MySQLi
好读
发布于 2013-01-06 14:19:40
在上,您将一个文本级元素a放入了一个块级元素td中,这个单元格显示了名字。你也没有在那里关闭a标签。正确的形式是这样的。
echo "<td align='center' style='vertical-align:text-top' width='200px'>";
echo "<a href='send.php'>" . $row['Firstname'] . "</a></td>";要在send.php上获得相同的用户bio,您需要传递此行的主键。例如,如果主键是id,则在查询字符串中传递send.php。
echo "<a href='send.php?id=".$row['id']."'>" . $row['Firstname'] . "</a></td>";现在,在send.php中,使用$_GET['id']获取主键,并使用它从db检索用户bio。
但是要确保你转义你传递给sql数据库的参数。不要直接使用这些变量!请参见Nullpointer的answer
更新1:
当您获得行的主键时,只需使用LIMIT 1调用SELECT *
$pkey = mysql_real_escape_string($_GET['id']);
$sql = "SELECT * FROM test where id='$pkey' LIMIT 1";
/* Run this sql */发布于 2013-01-07 02:38:40
这应该是你的第一页
<?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("simple_login", $con);
$result = mysql_query("SELECT * FROM test ");
echo "<table align='center' bgcolor='#F9F0F0' border='0' cellspacing='0'>
<tr>
<th><font color='red'>Firstname</font></th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td><a href='send.php'><img src='".$row['photo']."' \" width=\"150px\" height=\"150px\" /></a><br><br><br>";
echo "<a href='send.php?".$row['id']."'><td align='center' style='vertical-align:text-top' width='200px'>" . $row['Firstname'] . "</td>";
echo "<td align='center' style='vertical-align:text-top' width='200px'>" . $row['Lastname'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>现在send.php应该是
<?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("simple_login", $con);
$sql = "SELECT * FROM test where id = " . $_Get['id'] ;
$result = mysql_query($sql);
//then display the result here
?>希望这能有所帮助
https://stackoverflow.com/questions/14179765
复制相似问题