【问题标题】:PHP to transfer one users data over to another pagePHP将一个用户数据转移到另一个页面
【发布时间】:2011-11-22 23:18:15
【问题描述】:

我在电子商务网站中有一个管理区域,管理员可以在此查看 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、姓名、姓氏、地址 1、地址 2、城市、邮政编码、电子邮件、用户名、密码

产品 - 序列号、名称、描述、价格、图片

订单 - id、名称、数量、价格、日期、用户名

感谢您提供的任何帮助

【问题讨论】:

    标签: php mysql error-handling datatable


    【解决方案1】:

    您的代码缺少任何类型的安全机制...这非常糟糕,尤其是在电子商务环境中。

    因此,您可以将用户名传递给 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 . "'")
    

    【讨论】:

    • 如果您将 $info['id'] 替换为 $info['username'],此解决方案将有效
    【解决方案2】:

    一个简单的方法可能如下。替换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
    }
    

    请注意使用:

    1. mysql_real_escape_string() 函数的用户防止 Sql 注入(最好使用准备好的语句)
    2. 第一页使用参数username将用户名的值传递给第二页
    3. 使用$_GET全局数组检索参数

    【讨论】:

      【解决方案3】:

      试试这个:

      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>";
      ?>
      

      【讨论】:

      • 哦,伙计们,顺便说一句,我还有 1 个问题。正如您在 allusers.php 页面上看到的那样,我有一个表格,在表格中我有一行包含地址 1、地址 2、城市和邮政编码。这全部出现在一行上,我如何格式化它以使其彼此下显示而不将其放入另一列/行中?
      • 我所说的代码是这样的: echo "" 。 $info['address1']。 $info['address2']。 $信息['城市']。 $info['postcode']." ";
      • " .$info['address1'].'
        '.$info['address2'].'
        '.$info['city'].'
        '.$info['postcode']."
      猜你喜欢
      • 2022-06-27
      • 1970-01-01
      • 2018-10-29
      • 2013-05-16
      • 1970-01-01
      • 2012-01-03
      • 2016-03-10
      • 2017-03-02
      • 1970-01-01
      相关资源
      最近更新 更多