douzhan5058 2015-08-15 12:36
浏览 45
已采纳

将注释插入到表A中,并与表B链接,并在网页上显示它们

I have a function that is displaying the articles on my database.

There are two tables, one that holds the comments and one that holds the articles. I am connecting them through a foreign key.

function show_articles() { 
    include('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>;

            $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>;
        }
    }
}

This function is displaying the articles on the page, as well as comments for these articles. Comments are unique to the article they belong.

Now I am using the below function to insert a new comment for a specific article.

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

When I run the above function I get this error:

Cannot add or update a child row: a foreign key constraint fails (`DB1`.`article_comments`, CONSTRAINT `comment_blog_fk` FOREIGN KEY (`blog_id`) REFERENCES `blog` (`content_id`) ON DELETE NO ACTION ON UPDATE CASCADE) 

How can I insert a comment in my database that is linked to an article that the user is replying to? The foreign key field is called blog_id and it resides in article_comments table

EDIT: After applying

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);
}

How can I target $blog_id ? I run the below

<?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>

I have used 11 because I would like to target the 11th id of the blog article content and link it with the comments.

How can I do this without manually inserting the number?

  • 写回答

1条回答 默认 最新

  • dsgs8208 2015-08-15 13:45
    关注

    You must fill blog_id field on insert article_comments like this:

    function insert_comments($blog_id, $comment_by, $comment) {
        include('core/db/db_connection.php');
        $comment_by = sanitize($comment_by);
        $comment = sanitize($comment);
        $sql = "INSERT INTO article_comments (blog_id, comment_by, comment)
                VALUES ($blog_id, '$comment_by', '$comments')";
        mysqli_query($dbCon, $sql);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料