0

嗨,我正在尝试使用 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");
            }
        }
    }
}
4

2 回答 2

1

首先,您不必通过循环来查询 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-09T14:24:47.350 回答
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)

于 2013-11-09T13:53:32.317 回答