2013-05-07 70 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>'; 
    } 
相关问题