识别和连接表
之间ID的I有两个表 - 一个被存储文章内容,另一种是存储文章评论识别和连接表
我使用来显示这些功能是:
function list_articles() {
include('core/db/db_connection.php');
$sql = "SELECT blog.content_id, 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.blog_id
WHERE blog.content != ''
ORDER BY blog.content_id DESC";
$result = mysqli_query($dbCon, $sql);
$previous_blog_id = 0;
while ($row = mysqli_fetch_array($result)) {
if ($previous_blog_id != $row['content_id']) {
echo "<h5 class='posted_by'>Posted by {$row['posted_by']} on {$row['date']}</h5>
<h1 class='content_headers'>{$row['title']}</h1>
<article>{$row['content']}</article>
<hr class='artline'>";
$previous_blog_id = $row['content_id'];
}
if (!empty($row['comment_by']) && !empty($row['comments'])) {
echo "<div class='commented_by'>Posted by: {$row['comment_by']} </div>
<div class='comments'>Comments: {$row['comments']}</div>
<hr class='artline2'>";
}
}
}
我使用下面插入注释article_comments表:
function insert_comments($comments, $comment_by, $blog_id) {
include('core/db/db_connection.php');
$comment_by = sanitize($comment_by);
$comments = sanitize($comments);
$sql = "INSERT INTO article_comments (comments, comment_by, blog_id)
VALUES ('$comments', '$comment_by', '$blog_id')";
mysqli_query($dbCon, $sql);
}
这工作 - 它不插入,但我有我怎么能瞄准$ blog_id变量没有线索时,用户提交后...以下是我的形式使用
<?php echo list_articles();
if (!empty($_POST)) {
insert_comments($_POST['comments'], $_POST['username'], 11);
}
?>
<form method='post' action='' class='comments_form'>
<input type='text' name='username' placeholder='your name... *' id='name'>
<textarea name='comments' id='textarea' placeholder='your comment... *' cols='30' rows='6'></textarea>
<input type='submit' name='submit' id='post' value='post'>
</form>
我敢打赌,你注意到了我手动插入11作为最后一个变量设置了一个param。这链接到我的article_comments表中的blog_id
11(外键)。它显示评论就好。
有没有办法以$ blog_id为目标,而无需手动插入数字?类似于我如何使用$_POST['comments']
来定位$ comments变量?
此外,即使我可以瞄准,我怎么知道哪些帖子是用户评论?我应该给他们在下拉列表中选择的选项吗?这似乎很尴尬..但它是我能想到的唯一解决方案。
编辑:我在一个隐藏字段瞄准blog_id尝试:
function list_articles() {
include('core/db/db_connection.php');
$sql = "SELECT blog.content_id, 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.blog_id
WHERE blog.content != ''
ORDER BY blog.content_id DESC";
$result = mysqli_query($dbCon, $sql);
$previous_blog_id = 0;
while ($row = mysqli_fetch_array($result)) {
if ($previous_blog_id != $row['content_id']) {
echo "<h5 class='posted_by'>Posted by {$row['posted_by']} on {$row['date']}</h5>
<h1 class='content_headers'>{$row['title']}</h1>
<article>{$row['content']}</article>
<hr class='artline'>";
$previous_blog_id = $row['content_id'];
}
if (!empty($row['comment_by']) && !empty($row['comments'])) {
echo "<div class='commented_by'>Posted by: {$row['comment_by']} </div>
<div class='comments'>Comment: {$row['comments']}</div>
<hr class='artline2'>";
}
$sql2 = "SELECT FROM article_comments VALUES blog_id";
$result2 = mysqli_query($dbCon, $sql2);
while ($row = mysqli_fetch_assoc($result2)) {
echo " <form method='post' action='' class='comments_form'>
<input type='text' name='username' placeholder='your name... *' id='name'>
<textarea name='comments' id='textarea' placeholder='your comment... *' cols='30' rows='6'></textarea>
<input type='hidden' name=blog_id' value='{$row['blog_id']}'>
<input type='submit' name='submit' id='post' value='post'>
</form>";
}
}
}
SQL2和RESULT2部分是语句导致该错误的
编辑2:
我不认为$ sql2是正确的方法。现在的代码工作正常,但我回到了方块1.对于每个评论插入的文章得到重复。
<form method='post' action='' class='comments_form'>
<input type='text' name='username' placeholder='your name... *' id='name'>
<textarea name='comments' id='textarea' placeholder='your comment... *' cols='30' rows='6'></textarea>
<input type='hidden' name=blog_id' value='{$row['blog_id']}'>
<input type='submit' name='submit' id='post' value='post'>
</form>";
有没有办法为目标,而无需调用而($行= mysqli_fetch_array($结果)){}的blog_id?或者至少,不在第二个while循环中调用它?
随着第一段代码我贴我得到如下结果:
Article title: LOREM IPSUM
Content: LOREM IPSUM DOLOR SIT AMET....
--------------------------------------
Name: DSK
Comment: Great article!
--------------------------------------
Name: DSK
Comment: Great article! - 2nd comment
-- BEGIN SECOND ARTICLE ON WEBPAGE
Article title: LOREM IPSUM 2nd article
Content: LOREM IPSUM DOLOR SIT AMET....
--------------------------------------
Name: User0
Comment: Great article!
--------------------------------------
Name: User1
Comment: Great article! - 2nd comment
--------------------------------------
Name: User2
Comment: Great article! - 3rd comment
--------------------------------------
这正是我要找的。不过,我只能通过phpmyadmin接口插入注释,手动选择外键(blog_id)。
我希望能够通过一个形式得到相同的结果:
Article title: LOREM IPSUM
Content: LOREM IPSUM DOLOR SIT AMET....
-------------------------------------- //comments
Name: DSK
Comment: Great article!
--------------------------------------
Name: DSK
Comment: Great article! - 2nd comment
-------------------------------------- // end comments
|-------------------------------------| // comments form
|Name: New User |
|Comment: New comment ! |
| |
|-------------------------------------|
[Submit]
当用户提交表单时,他的名字和他的评论被提交到数据库到article_comments表。外键(blog_id)也应链接到现有文章(它的作用)。我只需要一种方法将其定位在我的功能中。
这是否有意义?....
也许你可以使用一个隐藏的表单元素表单里面:
<input type="hidden" name="blog_id" value="<?PHP echo $id;?>">
然后在提交你可以用$ _ POST访问[“blog_id”]
如果我理解正确你的问题是。
为什么在while循环中使用另一个查询$ sql2?不要声明另一个查询来查找博客ID,因为它已经在第一个查询中定义。直接在您的隐藏表单字段中使用 – kamal0808
这很有道理!谢谢 ! +1 – Dominique