How can I implement a comment function in a CMS using PHP?

To implement a comment function in a CMS using PHP, you can create a database table to store comments, a form for users to submit comments, and PHP code to handle the insertion and retrieval of comments from the database.

// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');

// Insert comment into the database
if(isset($_POST['submit_comment'])){
    $comment = $_POST['comment'];
    $stmt = $pdo->prepare("INSERT INTO comments (comment) VALUES (:comment)");
    $stmt->bindParam(':comment', $comment);
    $stmt->execute();
}

// Retrieve comments from the database
$stmt = $pdo->query("SELECT * FROM comments");
while($row = $stmt->fetch()){
    echo $row['comment'] . "<br>";
}

// HTML form for users to submit comments
echo '<form method="post" action="">
        <textarea name="comment"></textarea>
        <input type="submit" name="submit_comment" value="Submit">
      </form>';