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

まず第一に、ポストテーブルをクエリするためにループを通過する必要はありません。特定の投稿にコメントする場合は、その ID を隠しタイプの html 形式で渡します。

// 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 に答える