【发布时间】:2011-10-13 19:39:24
【问题描述】:
我需要这个以便使用登录的用户 ID,并且只有该用户可以使用 PHP 从 SQL 查询中查看他/她自己的数据,而不是所有数据我可以根据请求提供更多信息
(寻找
<center>
<title>J~Net Balance Accounts</title>
<?php
// Connects to your Database
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("messages") or die(mysql_error());
$data = mysql_query("SELECT * FROM users WHERE id=$id") <<<<
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>User:</th> <td>".$info['user_name'] . "</td> ";
Print "<th>Balance:</th> <td>".$info['balance'] . " </td></tr>";
}
Print "</table>";
?>
【问题讨论】:
-
你没有在任何地方定义
$id,是吗? -
$id设置在哪里?它输出所有行吗?如果$id未设置,您将在编写查询时遇到 sql 语法错误。 -
当用户登录时 - 保存他们的 id(或根据登录名/密码从数据库中提取)并将其存储在 Session (w3schools.com/php/php_sessions.asp) 中,然后在查询中使用它。也看看stackoverflow.com/questions/3180375/select-vs-select-column
-
问题出在哪里? $id 应该是用户的会话?
-
旁注,你的html有点乱,那里的'border'是什么,你为什么把'th'和'td'混在一起?