我有一个电子商务网站的管理区,借此管理员可以查看allusers.php页面上的所有用户。用户与他们的个人信息列在一个表中,但是我在每个用户附近有一个“查看个人资料”按钮,如果你点击它,它会带你到另一个页面,在那里你可以查看特定用户过去的订单。
以下是我为allusers.php编写的代码:
<?php
$result = mysql_query("SELECT * FROM customers ")
or die(mysql_error()); ;
if (mysql_num_rows($result) == 0) {
echo 'There Arent Any Orders Yet';
} else {
echo "<table border='0'><table border width=100%><tr><th>First Name</th><th>Surname</th><th>Address</th><th>E-Mail</th><th>Username</th><th>View Profile</th>";
while($info = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $info['name']. "</td>";
echo "<td>" . $info['surname']. "</td>";
echo "<td>" . $info['address1']. $info['address2']. $info['city']. $info['postcode']." </td>";
echo "<td>" . $info['email']. "</td>";
echo "<td>" . $info['username']. "</td>";
echo "<td>" . " <a href='view.php'>View</a> </td>";
}
}
echo "</tr>";
echo "</table>";
?>view.php页面如下所示:
<?php
$result = mysql_query("SELECT * FROM order WHERE ......dont know what to enter here")
or die(mysql_error()); ;
if (mysql_num_rows($result) == 0) {
echo 'There Arent Any Orders For This Customer Yet';
} else {
echo "<table border='0'><table border width=100%><tr><th>Product</th><th>Quantities</th><th>Date</th>";
while($info = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $info['name']. "</td>";
echo "<td>" . $info['quantity']. "</td>";
echo "<td>" . $info['date']. " </td>";
}
}
echo "</tr>";
echo "</table>";
?>我有一个mysql数据库,其中包含以下字段和表:
客户- id、姓名、姓氏、address1、address2、城市、邮政编码、电子邮件、用户名、密码
产品-系列,名称,描述,价格,图片
订单id、名称、数量、价格、日期、用户名
感谢您提供的帮助
发布于 2011-11-23 07:23:15
下面是一个简单的方法。在alluser.php中替换此行
echo "<td>" . " <a href='view.php'>View</a></td>";使用这一条
echo '<td><a href="view.php?username=' . $info['username'] . '">View</a></td>';然后,在你的view.php中有
if (isset($_GET['username']) && $_GET['username'] != '')
{
$username = mysql_real_escape_string($_GET['username']);
$result = mysql_query("SELECT * FROM order WHERE username = '$username'");
}
else
{
// No user specified. Do other statements
}请注意以下的用法:
mysql_real_escape_string()函数的用户免受Sql注入(最好使用准备好的statements)username将用户名的值传递到第二页$_GET全局数组检索参数发布于 2011-11-23 07:23:29
您的代码缺少任何类型的安全机制...这是非常糟糕的,尤其是在电子商务环境中。
除此之外,您还需要在URL中将用户名传递给视图页面。
echo "<td>" . " <a href='view.php?user=" . $info['username'] . "'>View</a> </td>";在视图页面中,您将从URL获取参数并将其包含在查询中。
if (isset($_GET) && isset($_GET['user'])) {
$user = mysql_real_escape_string($_GET['user']);
} else {
header('Location: allusers.php');
exit(); // boot them back to the previous page.
}
$result = mysql_query("SELECT * FROM order WHERE username = '" . $user . "'")发布于 2011-11-23 07:24:03
试试这个:
allusers.php
<?php
$result = mysql_query("SELECT * FROM customers ")
or die(mysql_error()); ;
if (mysql_num_rows($result) == 0) {
echo 'There Arent Any Orders Yet';
} else {
echo "<table border='0'><table border width=100%><tr><th>First Name</th><th>Surname</th><th>Address</th><th>E-Mail</th><th>Username</th><th>View Profile</th>";
while($info = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $info['name']. "</td>";
echo "<td>" . $info['surname']. "</td>";
echo "<td>" . $info['address1']. $info['address2']. $info['city']. $info['postcode']." </td>";
echo "<td>" . $info['email']. "</td>";
echo "<td>" . $info['username']. "</td>";
echo "<td>" . " <a href='view.php?user={$info['username']}'>View</a> </td>";
}
}
echo "</tr>";
echo "</table>";
?>view.php
<?php
$user = mysql_real_escape_string($_GET['user']);
$result = mysql_query("SELECT * FROM order WHERE user = '$user'")
or die(mysql_error()); ;
if (mysql_num_rows($result) == 0) {
echo 'There Arent Any Orders For This Customer Yet';
} else {
echo "<table border='0'><table border width=100%><tr><th>Product</th><th>Quantities</th><th>Date</th>";
while($info = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $info['name']. "</td>";
echo "<td>" . $info['quantity']. "</td>";
echo "<td>" . $info['date']. " </td>";
}
}
echo "</tr>";
echo "</table>";
?>https://stackoverflow.com/questions/8235298
复制相似问题