我写了一些简单的php代码,用于根据会话ID从数据库中检索数据。如果会话ID为1,那么我查询数据库以查找UserID为1的所有条目,然后输出它们。
这在一定程度上起作用,我可以让查询输出正确的条目,但由于某种原因它从不输出最新的条目,而是跳过它并输出其余条目。
我的代码在下面,有人能发现我做错了吗?
由于
$id = $_GET['id'];
if ($id)
{
$sql = "SELECT * FROM `forum_topics` WHERE `uid`='" . $id . "'";
$res = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($res) == 0)
{
echo "You haven't got any adverts to display";
}
else
{
$row = mysql_fetch_assoc($res);
echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\" width=\"100%\">\n";
echo "<tr align=\"center\"><td class=\"forum_header\">Title</td><td class=\"forum_header\">User</td><td class=\"forum_header\">Date Created</td></tr>";
while ($row2 = mysql_fetch_assoc($res))
{
echo "<tr align=\"center\"><td><a href=\"index.php?act=topic&id=" . $row2['id'] . "\">" . s($row2['title']) . "</a></td><td>" . uid($row2['uid']) . "</td><td>" . $row2['date'] . "</td></tr>";
}
echo "</table>\n";
}
}
答案 0 :(得分:2)
首先要做的事情
$id = $_GET['id'];
if ($id)
上面的代码有一个 SQL注入错误! 将它和所有代码改为无处不在
$id = mysql_real_escape_string($_GET['id']);
if ($id)
有关SQL注入的信息,请参阅:How does the SQL injection from the "Bobby Tables" XKCD comic work?
其次是你的问题
我认为您的问题 可能是交易。
您的用户的最新条目尚未提交。
这意味着虽然它尚未提交,但只有发布它的用户才能看到它(因为该条目在他的交易中)。
其他会话(包括您)中的人员在该用户的交易关闭之前才会看到该条目。
答案 1 :(得分:1)
在我看来,这条线是问题所在:
$row = mysql_fetch_assoc($res);
该行正在获取第一行,因此你的while循环从第二行开始(因为你已经获取了第一行,并且指针被移动到下一行)。
尝试删除我提到的行,看看它是否有效。