2013-07-21 37 views
0

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


     } 

?> 
+0

我想问题可能是在形式或点击出现另一个页面。另外,为什么在发布内容之前结束了你的''和''? – abiessu

回答

3

你需要定义$ _SESSION的$行,而不是_session $至$行

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

试试这个

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

或者可通过$通过ID _GET

在您的代码

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

,你可以得到这样的PRODUCT_ID中的其他文件

 echo $_GET['product_id'] ; 
+0

现在它给了我以下错误未定义的索引:在F:\ xampp \ htdocs \ CMS \ view_products.php上的id在线32 –

+0

'$ _GET'解决了我的问题:)谢谢! –

相关问题