Php 如何编写和显示特定产品的评论?
我正在做我的毕业设计电子商务网站 我希望每个用户能够评论不同的产品,并显示特定产品的评论,而不是一次显示所有评论。 到目前为止,我的评论代码是:Php 如何编写和显示特定产品的评论?,php,mysql,Php,Mysql,我正在做我的毕业设计电子商务网站 我希望每个用户能够评论不同的产品,并显示特定产品的评论,而不是一次显示所有评论。 到目前为止,我的评论代码是: <?php include("storescripts/init.php"); //contains DB connect and functions $username = mysql_real_escape_string($_POST['username']); $id=$_POST['id']; $co
<?php
include("storescripts/init.php"); //contains DB connect and functions
$username = mysql_real_escape_string($_POST['username']);
$id=$_POST['id'];
$comment = $_POST['comment'];
$submit = mysql_real_escape_string($_POST['submit']);
if (logged_in() == true) check ($SESSION['user_id'])
{
if ($submit){
if ($comment)
{$query = mysql_query("INSERT INTO `comments` (comment, username, id) VALUES ('$comment', '$username', '$id')");
$errors[] = 'You successfully posted a comment!';
}
else
{
$errors[] = '<span style="color:red;">Please write a comment first!</span>';
}
}
}else
{$errors[] = '<span style="color:red;">You must log in to post a comment!</span>';
}
?>
</h4>
<table width="400" border="0" bgcolor="#F8F8F8">
<tr>
<td>
<?php echo output_errors($errors) ?> //function to output the errors
<?php
$getquery = mysql_query("SELECT * FROM `comments` ORDER BY comment_id DESC");
while($rows=mysql_fetch_array($getquery))
{
$username =$rows['username'];
$comment_id = $rows['comment_id'];
$comment = $rows['comment'];
$date = $rows['date'];
$id = $rows['id'];
echo '<br>From : '. $username . ' ' . $date . '<br /><br /><em>' . $comment . '</em><hr>';
}
?>
<form action="" method="post">
<h4>Write a comment:</h4>
From:<br>
<input type="text" value="<?php echo $user_date['username']?>" size="15" name="username" readonly><br>
<input type="hidden" name="id" id="id" value="1" />
<textarea name="comment" cols="35" rows="5" id="comment"></textarea><br>
<input type="submit" name="submit" style=" background-color:orange; font-size:17px; border-radius:10px;" value="Post">
</form>
</td>
</tr>
</table>
//函数输出错误
写评论:
发件人:
在comments表中添加产品ID键,并为该键创建与products表ID字段的关系。然后仅加载与用户正在观看的产品ID匹配的商品
编辑
此外,您应该从本机mysql函数迁移到mysqli。
添加另一个字段product\u id,这样您就可以将它们链接起来。请注意,您使用的是不推荐的API。谢谢您的回答,非常有用!!只是我不知道如何编写if语句来检查id是否匹配。如果(某物){echo“thecomments”;}其他{echo”“;}???@Bobby从`comments`中选择*WHERE`product\u id`=comments\u id DESC
的任何顺序,您只需使用WHERE
子句使数据库仅在X与X匹配时减去项。在这种情况下,如果product id与用户正在监视的产品id匹配,则数据库将减去项。我想再次重申您从不推荐的mysql
函数切换到mysqli
函数的重要性。@Bobby很高兴能提供帮助!祝你毕业好运:)@Bobby Oh,如果没有其他问题,请按正确答案打分。