在PHP中显示和链接外键内容

时间:2015-08-15 09:04:05

标签: php mysql sql view

我正在参加一些在线课程,在其中一个练习中我们要为博客创建两个表 - 博客文章和博客文章 - 并通过外键连接它们,然后显示两者的所有内容。评论应仅与特定文章相关联,同时还允许多条评论。

我的尝试:

function list_articles() { 
    include('core/db/db_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.content_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>";
    }
}

这就是我在数据库中插入注释的方式:

function insert_comments($comment_by, $comments) {
    include('core/db/db_connection.php');
    $sql =  "SELECT blog.content_id, article_comments.blog_id  
             FROM blog AS blog
             INNER JOIN article_comments AS article_comments ON article_comments.blog_id > blog.content_id";
    mysqli_query($dbCon, $sql);
}

在PHPMyAdmin中,外键正常工作,注释与特定文章相关联。我想在网页上转置它。当我在页面上插入新文章时,它可以正常工作,但是当我尝试为该文章插入注释时,它将不会显示它。

如果我将ON blog.content_id = article_comments.content_id更改为ON blog.content_id = article_comments.blog_id(blog_id是外键的字段名称) - 它将显示文章的所有评论 - 但它会为与其关联的每个评论重复该文章。这有任何意义吗?我尽力解释它。如果你需要进一步澄清,请告诉我。感谢

顺便说一句,这是我用来创建外键的语句:

ALTER TABLE article_comments ADD CONSTRAINT comment_blog_fk FOREIGN KEY (blog_id) REFERENCES wt.blog(content_id) ON DELETE NO ACTION ON UPDATE CASCADE;

编辑:我使用ON blog.content_id = article_comments.blog_id

获得的结果
Article title: LOREM IPSUM
Content: LOREM IPSUM DOLOR SIT AMET....
--------------------------------------
Name: DSK
Comment: Great article!

-- HERE IT DUPLICATES THE ARTICLE TO INSERT A NEW COMMENT --

Article title: LOREM IPSUM
Content: LOREM IPSUM DOLOR SIT AMET....
--------------------------------------
Name: DSK
Comment: Great article! - 2nd comment

正如您所看到的,它会为插入的每条评论重复文章。所以我最终得到了两篇重复的文章,其中包含不同的评论。如果我有100条评论,那么该文章将被复制100次

我期待的行为:

Article title: LOREM IPSUM
Content: LOREM IPSUM DOLOR SIT AMET....
-------------------------------------- \\ COMMENTS \\
Name: DSK
Comment: Great article!
--------------------------------------
Name: DSK
Comment: Great article! - 2nd comment

1 个答案:

答案 0 :(得分:4)

试试这个:

body,
html {
  margin-top: 100px;
}
.navbar.navbar-custom {
  border-radius: 0px;
  border-left: transparent;
  background: #337ab7;
}
.navbar-custom .upper-nav {
  font-size: 20px;
  padding: 9px 20px;
  height: 50px;
  color: #337ab7;
  background-color: #f2f2f2;
}
.navbar-custom .upper-nav img {
  margin-top: 0px;
}
.navbar-custom .navbar-nav {
  margin-right: 30px;
}
.navbar-custom .nav-buttons {
  border-radius: 0px;
  background: none;
  border: none;
  color: #337ab7;
}
.btn-group .dropdown-menu > li > a {
  color: #fff;
}
.btn-group .dropdown-menu > li > a:hover {
  color: #444;
}
.navbar-custom .navbar-nav .dropdown-toggle {
  color: #fff;
}
.navbar-custom .navbar-nav > li > a {
  border-right: 1px solid #fff;
}
.navbar-custom .navbar-nav > li:first-child {
  border-left: 1px solid #fff;
}
.navbar-custom .navbar-header .navbar-brand {
  color: #fff;
}
.navbar-custom .dropdown-menu {
  background: #444;
}
.navbar-custom .navbar-nav .open .dropdown-menu > li > a,
.navbar-custom .navbar-nav .open .dropdown-menu {
  color: #fff;
  border-bottom: 1px solid #fff;
}
.navbar-custom .navbar-nav .open .dropdown-menu > li > a:hover,
.navbar-custom .navbar-nav .open .dropdown-menu:hover {
  color: #444;
}
.wrapper {
  height: auto;
  background-color: #fff;
  margin-top: 0px;
  padding: 10px 20px;
}
.well {
  background-color: transparent;
  border: 3px solid #428bca;
  border-radius: 0px;
  text-align: center;
  font-size: 25px;
}
@media (max-width: 768px) {
  .navbar-custom .navbar-nav {
    margin-right: 0;
  }
  .navbar-custom .navbar-nav > li > a {
    color: #fff;
    border: none;
  }
  .navbar-custom .navbar-nav > li:first-child {
    border: none;
  }
}