How can a comment function be effectively integrated into a PHP news script?

To integrate a comment function into a PHP news script, you can create a separate comments table in your database with a foreign key linking it to the news articles. Then, you can create a form on the news article page where users can submit comments, which will be inserted into the comments table. Finally, you can retrieve and display the comments associated with each news article on the page.

// Assuming you have a database connection established

// Insert comment into comments table
if(isset($_POST['submit_comment'])){
    $news_id = $_POST['news_id'];
    $comment = $_POST['comment'];

    $query = "INSERT INTO comments (news_id, comment) VALUES ('$news_id', '$comment')";
    $result = mysqli_query($connection, $query);
}

// Retrieve and display comments for a specific news article
$news_id = $_GET['id'];
$query = "SELECT * FROM comments WHERE news_id = '$news_id'";
$result = mysqli_query($connection, $query);

while($row = mysqli_fetch_assoc($result)){
    echo $row['comment'];
}