我正在尝试使用php和mysql制作评论系统(没有jquery或ajax) 问题是如何找到用户评论我使用了一个while循环的帖子的id,因为它发布到所有帖子到目前为止我在这里.....
//user data is set
if (isset($_POST['comment'])) {
//variables to post in database
$comment = $_POST['comment'];
$com_from = $_SESSION['user'];
$com_to = $_GET['u'];
$com_time = date("Y-m-d H:i:s");
$u = $_GET['u'];
//query to get the id of the post in the `post` table
$que = mysql_query("SELECT * FROM posts WHERE `post_to` = '$u'");
if ($que) {
//loop through all the posts ad get all ID
while ($ro = mysql_fetch_array($que)) {
$pst_id = $ro['post_id'];
//query inside the while loop for getting the post ID i think here is the problem
if (!empty($_POST['comment'])) {
$com_query = mysql_query("INSERT INTO comments SELECT '','$comment',`post_id`,'$com_from','$com_to','$com_time' FROM `posts` WHERE `posts`.`post_id` = $pst_id");
}
}
}
}
答案 0 :(得分:1)
首先,你不必通过循环来查询帖子表。 如果您正在评论特定帖子,则以隐藏类型的html格式传递其ID。
// here 1 is id of post
<input type="hidden" name="postid" value="1">
然后你可以编写插入查询,如:
if (isset($_POST['comment'])) {
//variables to post in database
$comment = $_POST['comment'];
$com_from = $_SESSION['user'];
// $com_to is post id and i believe comment table contain field to store post id
$com_to = $_POST['postid'];
$com_time = date("Y-m-d H:i:s");
$que = mysql_query("INSERT INTO comments VALUES('$comment','$com_to','$com_from','$com_time')");
}
答案 1 :(得分:0)
我提供了有限理解的插入查询,
$pst_id = $_POST['postid']; // from form
$com_query = mysql_query("INSERT INTO comments values ('$comment','$pst_id','$com_from','$com_to','$com_time') ");
如果您共享comments
表格结构,我可以为您提供更多帮助。
注意使用mysqli_ *函数代替mysql_ *函数(deprecated)