php - 如何从数据库中选择任何特定项目并将其显示在不同的页面中

标签 php mysql database mysqli singlepage

我正在创建一个程序,其中有一个产品列表。我希望用户单击任何特定产品并查看其详细信息。我写了下面的代码。我将当前的“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>';


        }

?>

最佳答案

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

     $_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'] ;

关于php - 如何从数据库中选择任何特定项目并将其显示在不同的页面中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17772012/

相关文章:

python - 如何使用flask.jsonify 通过查询数据库返回嵌套的json?

c# - .CSV 到 SQL CE 表?

mysql创建数据库名称错误

php - 如何制作可点击的 map 悬停效果?

javascript - 跨域 AJAX 请求在 IE10 上不起作用

PHP 表单复选框

php - 在python中对以整数开头的json数组进行排序

php - 在 Laravel 中加入 where 查询返回错误的时间戳

php - 这叫什么,我应该阅读什么才能让它发挥作用?

mysql - 无法多次调用Mysql存储过程,存在于同一个sql文件中