识别并连接表之间的ID

时间:2015-08-15 14:39:32

标签: php mysql

我有两个表 - 一个是存储文章内容,另一个是存储文章注释

我用来显示这些的功能是:

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作为最后一个变量的参数。这链接到我的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>";

有没有办法在没有调用while($ row = mysqli_fetch_array($ result)){}的情况下定位blog_id?或者至少在第二次循环中没有调用它?

使用我发布的第一段代码,我得到以下结果:

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)也应链接到现有文章(它所做的)。我只需要一种方法来在我的函数中定位它。

这有意义吗?....

1 个答案:

答案 0 :(得分:1)

也许您可以在表单中使用隐藏的表单元素:

<input type="hidden" name="blog_id" value="<?PHP echo $id;?>">

然后在提交时你可以用$ _POST访问它[&#34; blog_id&#34;]

如果我理解你的问题是正确的。