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?