我正在尝试用php和mysql (没有jquery或ajax)做一个评论系统,问题是如何找到用户评论的帖子的id,我使用了while循环,因为它张贴到所有的帖子,到目前为止我在这里.....
//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");
}
}
}
}发布于 2013-11-09 22:24:47
首先,你不必通过循环来查询post表。如果你正在评论一个特定的帖子,用隐藏类型在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')");
}发布于 2013-11-09 21:53:32
我以有限的理解提供了insert查询,
$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)
https://stackoverflow.com/questions/19876590
复制相似问题