【问题标题】:How to select any specific item from Database and show it in a different page如何从数据库中选择任何特定项目并将其显示在不同的页面中
【发布时间】:2013-07-21 11:49:46
【问题描述】:

我正在创建一个包含产品列表的程序。我希望用户单击任何特定产品并查看其详细信息。我已经编写了以下代码。我将当前的 'product_id' 抓取到 $_SESSION 变量中,并在下一页中显示其详细信息。它的工作,但没有给我想要的输出,它为每个产品获取相同的 id。我认为这是因为我将其值存储在 $_SESSION 中。请检查并告诉我正确的操作方法。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

<?php 
session_start();

    include 'connect.php';

        $query=         mysql_query ("select product_id,name, price, description from products" );


        while ($query_array=    mysql_fetch_assoc($query))
        {
            echo '</br><pre>';





            $_SESSION['id']=    $query_array['product_id'];
            $product_id=     $_SESSION['id'];           

            echo "<a href='single_product.php?product_id=$product_id' >";
            print_r ($query_array['name']);
            echo    "</a>";

            print_r ($query_array['price']);

            echo "<img src= 'all_images.php' alt= 'Image'";

            echo '</pre>';


        }

?>

【问题讨论】:

  • 我认为问题可能出在表单或点击发生的另一个页面上。另外,您为什么在发布内容之前结束了您的&lt;body&gt;&lt;html&gt;

标签: php mysql database mysqli singlepage


【解决方案1】:

您需要在 $_session 中定义 $row 而不是在 $row 中定义 $_session

     $_SESSION['id']=    $query_array['product_id'];   //<---- must reverse it.
        $product_id=     $_SESSION['id']; 

试试这个

       $product_id = $query_array['product_id'] ;
       $product_id = $_SESSION['id'];

或者您可以通过 $_GET 传递 ID

在您的代码中

   echo "<a href='single_product.php?product_id=$product_id' >";

您可以像在其他文件中那样获取 product_id

     echo $_GET['product_id'] ;

【讨论】:

  • 现在它给了我以下错误 Undefined index: id in F:\xampp\htdocs\CMS\view_products.php on line 32
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多