2013-07-21 35 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'解決了我的問題:)謝謝! –

相關問題