我有以下功能,显示来自数据库的博客文章:
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不匹配,即使没有文章可以发表,用户仍然可以发表评论。
我还编写了以下函数来获取comments表的ID和articles表的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\u id。所以,让您也在函数中传递内容id。我对您的insert_comments
功能做了一些更改。在insert命令中不需要where子句,因为在insert命令中不支持它这是密码。
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);
}
当您将
username
传入get_article_id
时,让您根据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'));
}
您必须传递
username
而不是comment_id
中的get_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 - PHP/MySqli根据表B ID检查表ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32018495/