我正在做我的毕业设计电子商务网站http://www.fieldhockey-world.co.uk 我希望每个用户都能够评论不同的产品,并显示特定产品的评论,而不是一次显示所有评论。 到目前为止,我的评论代码是:
<?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>
我的MySQL表是:
Tabel评论
comment_id int / 评论文字/ 用户名varchar / 日期时间戳
如果您有任何想法,请告诉我。 谢谢!!
答案 0 :(得分:0)
在评论表中添加产品ID密钥,并为该产品表ID字段创建该密钥的关系。然后只加载与用户正在观看的产品ID匹配的模式。
修改强>
此外,您应该从本机mysql函数迁移到mysqli。 PHP.net about Mysqli