gpt4 book ai didi

php - 如何进行查询以获取特定的帖子 ID?

转载 作者:行者123 更新时间:2023-11-29 13:28:00 25 4
gpt4 key购买 nike

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

最佳答案

首先,您不必经历查询帖子表的循环。如果您正在评论特定帖子,请以隐藏类型的 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')");
}

关于php - 如何进行查询以获取特定的帖子 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19876590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com