gpt4 book ai didi

PHP/MySqli 检查表 A ID 与表 B ID

转载 作者:行者123 更新时间:2023-11-29 02:54:37 27 4
gpt4 key购买 nike

我有以下功能,它显示来自数据库的博客文章:

function show_blog_posts() { 
include('connection.php');
$sql = "SELECT blog.title, blog.content, blog.posted_by, blog.date, article_comments.comments, article_comments.comment_by
FROM blog LEFT OUTER JOIN article_comments
ON blog.content_id = article_comments.comment_id
WHERE blog.content != ''
ORDER BY blog.content_id DESC";
$result = mysqli_query($dbCon, $sql);
while ($row = mysqli_fetch_array($result)) {
echo
"<h5 class='posted_by'>Posted by " . $posted_by = $row['posted_by'] . " on " . $row['date'] . "</h5>" .
"<h1 class='content_headers'>" . $title = $row['title'] . "</h1>" .
"<article>" . $content = $row['content'] . "</article>" .
"<div class='commented_by'>Posted by: " . $row['comment_by'] . "</div>" .
"<div class='comments'>Comments: " . $row['comments'] . "</div>";
}
}

如您所见,我有两张表,一张是评论,一张是文章。

我希望能够对这些博客文章实现评论功能,我正在使用以下函数将这些评论插入表 B。

function insert_comments($comment_by, $comments) {
include('db_connection.php');
$sql = "INSERT INTO article_comments (comments, comment_by) VALUES ('$comments', '$comment_by') WHERE article_comments.comment_id = blog.content_id";
mysqli_query($dbCon, $sql);
}

我正在尝试将表 A 的 ID 与表 B 的 ID 链接起来,因此如果 ID 不匹配,用户将无法发帖。如果我与 ID 不匹配,即使没有要发布的文章,用户仍然可以发表评论。

我还编写了以下函数来获取评论表的 ID 和文章表的 ID

function get_article_id($username) { 
include('db_connection.php');
$username = sanitize($username);
$sql = "SELECT content_id FROM `blog` WHERE content_id = '$content_id'";
$query = mysqli_query($dbCon, $sql);
return (mysqli_result($query, 0, 'content_id'));
}

function get_comment_id($comment_id) {
include('db_connection.php');
$comment_id = sanitize($username);
$sql = "SELECT comment_id FROM `article_comments` WHERE comment_id = '$comment_id'";
$query = mysqli_query($dbCon, $sql);
return (mysqli_result($query, 0, 'comment_id'));
}

如何检测表 A 的 ID 并将其与表 B 的 ID 进行比较?如果一个

此外,我的代码有什么问题,因为 sql 查询没有执行。

最佳答案

首先我想告诉你一个博客可以有多个评论。因此,您需要仔细处理与评论表的关系。

article_comments 表将有一个外键 ID 作为 "content_id"

现在将您的 show_blog_posts 函数的查询更改为没有评论关系的类似内容,然后使用另一个查询在 while 循环中获取其评论

这是代码。

function show_blog_posts()
{
include('connection.php');
$sql = "SELECT blog.title, blog.content, blog.content_id, blog.posted_by, blog.date
FROM blog
WHERE blog.content != ''
ORDER BY blog.content_id DESC";
$result = mysqli_query($dbCon, $sql);

if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
echo
"<h5 class='posted_by'>Posted by " . $posted_by = $row['posted_by'] . " on " . $row['date'] . "</h5>" .
"<h1 class='content_headers'>" . $title = $row['title'] . "</h1>" .
"<article>" . $content = $row['content'] . "</article>";

$sql = "SELECT article_comments.comments, article_comments.comment_by
FROM article_comments
WHERE article_comments.content_id =" . $row['content_id'] . "
ORDER BY article_comments.comment_id DESC";
$comments = mysqli_query($dbCon, $sql);

echo "<div>"; //comments panel
if (mysqli_num_rows($result) > 0) {
while ($comment = mysqli_fetch_array($comments)) {
echo "<div class='commented_by'>Posted by: " . $comment['comment_by'] . "</div>";
echo "<div class='comments'>Comments: " . $comment['comments'] . "</div>";
}
}
echo "</div>"; //comments panel ends
}
}

}

要向 article_comments 表插入评论,您还必须在查询中指定 content_id。因此,让您在函数中也传递 content_id。我对您的 insert_comments 函数做了一些更改。插入命令中不需要 where 子句,因为插入命令不支持它

这是代码。

function insert_comments($comment_by, $comments, $content_id) {
include('db_connection.php');
$sql = "INSERT INTO article_comments (content_id, comments, comment_by) VALUES ($content_id, '$comments', '$comment_by');";
mysqli_query($dbCon, $sql);
}

当您在 get_article_id 中传递 username 时,让您根据 username 而不是 content_id 进行比较。

function get_article_id($username) {
include('db_connection.php');
$username = sanitize($username);
//if username is unique
$sql = "SELECT content_id FROM `blog` WHERE username = '$username'";
$query = mysqli_query($dbCon, $sql);
return (mysqli_result($query, 0, 'content_id'));
}

您必须在 get_comment_id 中传递 username 而不是 comment_id,因为您要检索 comment_id。当您知道 comment_id 时,就不需要调用此函数。我已经相应地改变了它。

function get_comment_id($username) {
include('db_connection.php');
$username = sanitize($username);
$sql = "SELECT comment_id FROM `article_comments` WHERE username = '$username'";
$query = mysqli_query($dbCon, $sql);
return (mysqli_result($query, 0, 'comment_id'));
}

关于PHP/MySqli 检查表 A ID 与表 B ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32018495/

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