2017-08-13 24 views
1

我有一個PHP頁面,其產品列表如下所示。最後一列是鏈接到單個產品頁面的鏈接,報告有關該產品的所有詳細信息。Mysql/Php - 顯示從列表中選擇的單個產品頁面

ID  | Title  | LINKTOPAGE 
-------|--------------|------------------ 
001 | ProductTitle | $ID(001)-->Link to the single_product_page.php 
002 | ProductTitle | $ID(002)-->Link to the single_product_page.php 
003 | ProductTitle | $ID(003)-->Link to the single_product_page.php 

上表是由下面的循環產生:

<?php 
      while ($row = mysqli_fetch_array($risultato)){ 
       echo '<tr> 
         <td>'.$row['id'].'</td> 
         <td>'.$row['post_title'].'</td> 
         <td>'.'LinkToSinglePage >>'.'</td> 
        </tr>'; 
      }?> 

我已經準備從該查詢檢索數據的單頁:

SELECT 
    p.id as ID, 
    p.post_title, 
    p.post_status, 
    p.post_excerpt as shortdesc, 
    p.post_content as longdesc 
    FROM mg_posts as p 
    WHERE p.ID= 13323 
    GROUP BY p.ID 

什麼我想是否將「linktoProductPaGe」鏈接到單個頁面。如果我沒有錯,我應該用存儲所選產品的ID的變量替換ID = 13323。

任何建議如何?

+0

是你需要更換 –

+0

用事先準備好的聲明中,還可以使用較後ID的變量。 –

+0

Tim,我想念的是如何將products_lists.php的變量(ID產品)傳遞給single_product_page.php的p.ID,如示例'13323'中所示。 – Alex

回答

1

你可以不喜歡它,例如(get方法):

產品列表(products_lists.php):

<?php 
     while ($row = mysqli_fetch_array($risultato)){ 
      echo '<tr> 
        <td>'.$row['id'].'</td> 
        <td>'.$row['post_title'].'</td> 
        <td>single_product_page.php?product_id='.$row['id'].'</td> 
       </tr>'; 
     }?> 

而在你的商品詳細頁(single_product_page.php),你可以得到這樣的ID:

<?php $product_id = $_GET['product_id']; ?> 

希望這會有所幫助。 :-)

+0

想要嘗試它拉斐爾。 – Alex

+0

而你應該通過使用PDO來防止SQL查詢。 ;-) –

+0

對我來說不容易,但我會嘗試。 – Alex

0

爲了避免在瀏覽器中打開頁面single_product_page.php時有一個語法錯誤,你應該添加以下代碼:

if(!isset($_GET['product_id']){ 
    echo 'No product selected.'; 
}else{ 
    // Your code to get the product infos 
} 
+0

在else部分中,我將包含用於檢索信息的sql查詢,對不對? – Alex

+0

問題在於被置於single_product_page的字段中並按下了ENTER鍵,腳本沒有找到$ product_id變量並提供給我這個錯誤: - >「注意:未定義的索引:product_id in/Applications第7行的/XAMPP/xamppfiles/htdocs/gest/pages/singolo_prodotto.php 查詢非valida:您的SQL語法中有錯誤;請檢查與您的MariaDB服務器版本對應的手冊,以找到正確的語法以在'GROUP通過p.ID聯盟所有選擇ps.id,ps.post_title,ps.post'在第22行<--- – Alex

0

這可能是因爲你的HTML的標記「行動」表單要麼沒有定義,要麼具有「singolo_prodotto.php」的值。

所以,在你的頁面「singolo_prodotto.php」的PHP代碼的開始,你應該添加以下代碼,如進一步解釋了起來:

if(!isset($_GET['product_id']){ 
    echo 'No product selected.'; 
}else{ 
    // Your code to get the product infos 
} 

而當你將不得不管理該產品的版本,你就必須添加其他條件類似:

if(!isset($_GET['a_field_of_your_html_form']){ // Or "$_POST['a_field_of_your_html_form']" 
    // Your product edition code 
} 
+0

你試過了嗎?它工作嗎? –

相關問題