2013-05-07 72 views
1

我想使用PHP和MySQL製作評論系統。我做了一個名爲「註釋」表中存儲的細節在mysql的一列中選擇多個具有相同值的行?

 
id | date  |product_id | user_name | email_id   | comment 
    |   |   |   |     | 
1 |2013-05-07 | 10001 | jabong | [email protected] | this is good product 
2 |2013-05-07 | 10001 | john  | [email protected] | I bought this product 


現在我想選擇和打印所有那些具有的product_id = 10001行。 我使用下面的代碼打印細節

$comment=""; 
    $id=$_GET['id'];//this is the product_id which is taken from URL 
    $query=mysql_query("SELECT * FROM comment WHERE product_id='$id'"); 
    while($data = mysql_fetch_array($query)) 
    { 
     $name = $data["user_name"]; 
     $email = $data["email_id"]; 
     $comment = $data["comment"]; 
     $date=strftime("%d %b, %Y", strtotime($data["date"])); 

     $comment.='<table> 
         <tr> 
         <td>Date: '.$date.'</td> 
         </tr> 
         <tr> 
         <td>Name: '.$name.'</td> 
         </tr> 
         <tr> 
         <td>Email_id: '.$email.'</td> 
         </tr> 
         <tr> 
         <td>Product Review: '.$comment.'</td> 
         </tr> 
        </table><hr />'; 

,然後我回響着他們的主體部分。但我只得到一個結果。 所以請幫助我獲得正確的代碼。

+0

爲什麼多次使用'$ comment'變量,它必須被覆蓋。嘗試給出不同的變量名稱。 – Rikesh 2013-05-07 16:08:34

+0

您是否嘗試過在phpMyAdmin中的sql請求,例如...您有幾個結果? – mlwacosmos 2013-05-07 16:10:32

回答

0

你有這樣一行:

$comment = $data["comment"]; 

是改寫了這一個:

$comment.='<table> 
         <tr> 
         <td>Date: '.$date.'</td> 
         </tr> 
         <tr> 
         <td>Name: '.$name.'</td> 
         </tr> 
         <tr> 
         <td>Email_id: '.$email.'</td> 
         </tr> 
         <tr> 
         <td>Product Review: '.$comment.'</td> 
         </tr> 
        </table><hr />'; 

所以當你正在做的echo $comment它顯示最後一個。

改變變量和DONE

類似:

$comment_sql = $data["comment"]; 
.... 
    $comment.='<table> 
          <tr> 
          <td>Date: '.$date.'</td> 
          </tr> 
          <tr> 
          <td>Name: '.$name.'</td> 
          </tr> 
          <tr> 
          <td>Email_id: '.$email.'</td> 
          </tr> 
          <tr> 
          <td>Product Review: '.$comment_sql.'</td> 
          </tr> 
         </table><hr />'; 
... 
+0

我更正了我的腳本根據你。但它沒有解決問題。我只從select語句獲得一條記錄。 – Pankaj 2013-05-08 04:46:17

+0

謝謝大家哦!完成 !! 對變量variiables進行更改後,我沒有使用連接符號!現在完成了! – Pankaj 2013-05-08 05:37:20

0

首先最重要的是,清理你的數據。

$id=mysql_real_escape_string(trim($_GET['id'])); 
$query=mysql_query("SELECT * FROM comment WHERE product_id='$id'"); 

而($數據= mysql_fetch_array($查詢))

第二個問題是要覆蓋在每個循環變量$評論,所以你只得到1分的記錄,即使你是循環兩次。

remove this: $comment = $data["comment"]; 
change this: <td>Product Review: '.$comment.'</td> 
to this: <td>Product Review: '.$data["comment"].'</td> 
+0

感謝你們所有人的建議。我完全按照你在這裏所說的做了。但無法獲得解決方案。我只得到一個記錄打印。我不知道發生了什麼事? – Pankaj 2013-05-08 04:45:00

+0

你能告訴我你的更新代碼嗎? – 2013-05-08 19:01:30

1

有一個小問題,我的代碼。我使用了兩次相同的變量,也沒有使用連接符號。但現在它解決了---

 $review=""; 
     if (isset($_GET['id'])) 
    { 
     $id=mysql_real_escape_string(trim($_GET['id'])); 
     $query=mysql_query("SELECT * FROM comment WHERE product_id='$id'"); 
     $review.='<table width="70%" cellpadding="6px" cellspacing="0" >'; 
     while($data=mysql_fetch_array($query)) 
     { 
      $name = $data["user_name"]; 
      $email = $data["email_id"]; 
      $comment = $data["comment"]; 
      $date=strftime("%d %b, %Y", strtotime($data["date"])); 
      $review.='<tr> 
         <td> 
          <table width="100%" border="0" style="border:1px solid; border-color:#05fdee; background-color:#e4f2f1;"> 
           <tr> 
           <td rowspan="2" width="100px"> <img src="profile.png" /> </td> 
           <td align="left"><b> '.$name.' </b> says -</td> 
           <td align="right"><b> '.$date.' </b></td> 
           </tr> 
           <tr> 
           <td colspan="2">'.$comment.'</td> 
           </tr> 
          </table> 
         </td> 
         </tr>'; 

     }// while loop ends here 
     $review.='</table>'; 
    } 
相關問題