• We just launched and are currently in beta. Join us as we build and grow the community.

Creating a Simple Comment Section using PHP and SQLite Tutorial

Mortalapi

Banana Technician
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
120
Likes
169
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, you will learn how to create a Simple Comment Section in your Web Application Projects using PHP and SQLite Database. Comment Section is one of the most common feature in a web application such as Blog Sites. This application feature allows users to leave a comment, ask questions, or feedback for a certain post/content. This kind of web application feature also allows other users to reply-to-comment which means other users can leave a comment under a specific comment too.

Here, I will be providing a very simple comment section web application in which app users can submit, read, reply, edit, and delete comments. The demo application that we will be creating don not require users to register and log in, this only requires entering the user name for each comment and reply.

Getting Started

In this tutorial source code, I used Bootstrap v5 for the design of application and XAMPP v3.3.0. Download and Install the said library and local web machine server to run the following script on your end. After Installing the XAMPP, open the XAMPP's php.ini

file (i.e "C:\xampp\php\php.ini") in a text-editor and uncomment the ;extension=sqlite3

. To do that, remove the (;

) [semicolon]. Lastly, open your XAMPP's Control Panel and start the Apache

Creating the Database Connection

The follwing PHP scripts is the database connection script of our project. This also contains a script that creates the database and tables if not yet existed. Save this file as Connection.php

  1. <?php
  2. session_start

    (

    )

    ;
  3. if

    (

    !

    is_dir

    (

    __DIR__.

    '/db'

    )

    )
  4. mkdir

    (

    __DIR__.

    '/db'

    )

    ;
  5. if

    (

    !

    defined

    (

    'db_file'

    )

    )

    define

    (

    'db_file'

    ,

    __DIR__.

    './db/comment_db.db'

    )

    ;
  6. function

    my_udf_md5(

    $string

    )

    {
  7. return

    md5

    (

    $string

    )

    ;
  8. }
  9. Class

    DBConnection extends

    SQLite3{
  10. protected

    $db

    ;
  11. function

    __construct(

    )

    {
  12. $this

    ->

    open

    (

    db_file)

    ;
  13. $this

    ->

    createFunction

    (

    'md5'

    ,

    'my_udf_md5'

    )

    ;
  14. $this

    ->

    exec

    (

    "PRAGMA foreign_keys = ON;"

    )

    ;

  15. $this

    ->

    exec

    (

    "CREATE TABLE IF NOT EXISTS `comments` (
  16. `comment_id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  17. `sender` TEXT NOT NULL,
  18. `comment` TEXT NOT NULL,
  19. `date_created` TIMESTAMP DEFAULT (datetime('now','localtime'))
  20. )"

    )

    ;

  21. $this

    ->

    exec

    (

    "CREATE TABLE IF NOT EXISTS `replies` (
  22. `reply_id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  23. `comment_id` INTEGER NOT NULL,
  24. `sender` TEXT NOT NULL,
  25. `reply` TEXT NOT NULL,
  26. `date_created` TIMESTAMP DEFAULT (datetime('now','localtime')),
  27. FOREIGN KEY (`comment_id`) References `comments`(comment_id) ON DELETE CASCADE
  28. )"

    )

    ;

  29. }
  30. function

    __destruct(

    )

    {
  31. $this

    ->

    close

    (

    )

    ;
  32. }
  33. }

  34. $conn

    =

    new

    DBConnection(

    )

    ;

Creating the Page Interfaces

The below source code files is a PHP files/scripts that contains the codes of each pages interfaces. In contains a combined HTML and PHP scripts. Save the following files according to the given filename above each file scripts.

index.php
This file contains the interface script of the application's Main Page. This contains also the query that list all the comments and replies submitted.

  1. <?php

    require_once

    (

    'Connection.php'

    )

    ?>
  2. <!DOCTYPE html>
  3. <html lang="en">

  4. <head>
  5. <meta charset="UTF-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>Simple Comment Section</title>
  9. <link rel="stylesheet" href="./css/bootstrap.min.css">
  10. <script src="./js/jquery-3.6.0.min.js"></script>
  11. <script src="./js/bootstrap.min.js"></script>
  12. <style>
  13. :root {
  14. --bs-success-rgb: 71, 222, 152 !important;
  15. }

  16. html,
  17. body {
  18. height: 100%;
  19. width: 100%;
  20. font-family: Apple Chancery, cursive;
  21. }
  22. </style>
  23. </head>

  24. <body class="bg-light">
  25. <nav class="navbar navbar-expand-lg navbar-dark bg-primary bg-gradient" id="topNavBar">
  26. <div class="container">
  27. <a class="navbar-brand" href="https://sourcecodester.com">
  28. Sourcecodester
  29. </a>
  30. </div>
  31. </nav>
  32. <div class="container py-3" id="page-container">
  33. <h3>Simple Comment Section</h3>
  34. <?php

    if

    (

    isset

    (

    $_SESSION

    [

    'response_msg'

    ]

    )

    )

    :

    ?>
  35. <div class="alert alert-<?php

    echo

    $_SESSION

    [

    'response_type'

    ]

    ?>

    ">
  36. <p class="m-0"><?php

    echo

    $_SESSION

    [

    'response_msg'

    ]

    ?>

    </p>
  37. </div>
  38. <?php
  39. unset

    (

    $_SESSION

    [

    'response_msg'

    ]

    )

    ;
  40. unset

    (

    $_SESSION

    [

    'response_type'

    ]

    )

    ;
  41. endif

    ;
  42. if

    (

    isset

    (

    $_SESSION

    [

    'comment-post'

    ]

    )

    )
  43. unset

    (

    $_SESSION

    [

    'comment-post'

    ]

    )

    ;
  44. if

    (

    isset

    (

    $_SESSION

    [

    'reply-post'

    ]

    )

    )
  45. unset

    (

    $_SESSION

    [

    'reply-post'

    ]

    )

    ;
  46. ?>
  47. <hr>
  48. <?php
  49. $comment_qry

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `comments` order by strftime(date_created) asc "

    )

    ;
  50. while

    (

    $row

    =

    $comment_qry

    ->

    fetchArray

    (

    )

    )

    :
  51. ?>
  52. <div class="card mb-2">
  53. <div class="card-body">
  54. <div class="d-flex align-items-end">
  55. <div class="fw-bold flex-grow-1"><?php

    echo

    ucwords

    (

    $row

    [

    'sender'

    ]

    )

    ?>

    </div>
  56. <span><small class="text-muted"><?php

    echo

    date

    (

    "m d,Y h:i A"

    ,

    strtotime

    (

    $row

    [

    'date_created'

    ]

    )

    )

    ?>

    </small></span>
  57. </div>
  58. <hr>
  59. <div class="lh-1">
  60. <p class=""><span class="mx-3"></span><?php

    echo

    str_replace

    (

    '\n'

    ,

    '<br/>'

    ,

    $row

    [

    'comment'

    ]

    )

    ?>

    </p>
  61. </div>
  62. <hr>
  63. <div class="w-100 d-flex justify-content-end">
  64. <a href="reply.php?comment_id=<?php

    echo

    $row

    [

    'comment_id'

    ]

    ?>

    " class="btn btn-primary btn-sm rounded-0 me-2">Reply</a>
  65. <a href="edit_comment.php?comment_id=<?php

    echo

    $row

    [

    'comment_id'

    ]

    ?>

    " class="btn btn-primary btn-sm rounded-0 me-2">Edit</a>
  66. <a href="delete_comment.php?comment_id=<?php

    echo

    $row

    [

    'comment_id'

    ]

    ?>

    " class="btn btn-danger btn-sm rounded-0">Delete</a>

  67. </div>
  68. </div>
  69. </div>
  70. <?php
  71. $reply_qry

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `replies` where comment_id ='{$row['comment_id']}

    ' order by strftime(date_created) asc "

    )

    ;
  72. while

    (

    $rrow

    =

    $reply_qry

    ->

    fetchArray

    (

    )

    )

    :
  73. ?>
  74. <div class="card mb-2" style="margin-left:15%">
  75. <div class="card-body">
  76. <div class="d-flex align-items-end">
  77. <div class="fw-bold flex-grow-1"><?php

    echo

    ucwords

    (

    $rrow

    [

    'sender'

    ]

    )

    ?>

    </div>
  78. <span><small class="text-muted"><?php

    echo

    date

    (

    "m d,Y h:i A"

    ,

    strtotime

    (

    $rrow

    [

    'date_created'

    ]

    )

    )

    ?>

    </small></span>
  79. </div>
  80. <hr>
  81. <div class="lh-1">
  82. <p class=""><span class="mx-3"></span><?php

    echo

    str_replace

    (

    '\n'

    ,

    '<br/>'

    ,

    $rrow

    [

    'reply'

    ]

    )

    ?>

    </p>
  83. </div>
  84. <hr>
  85. <div class="w-100 d-flex justify-content-end">
  86. <a href="reply.php?reply_id=<?php

    echo

    $rrow

    [

    'reply_id'

    ]

    ?>

    &comment_id=<?php

    echo

    $row

    [

    'comment_id'

    ]

    ?>

    " class="btn btn-primary btn-sm rounded-0 me-2">Edit</a>
  87. <a href="delete_reply.php?reply_id=<?php

    echo

    $rrow

    [

    'reply_id'

    ]

    ?>

    " class="btn btn-danger btn-sm rounded-0">Delete</a>
  88. </div>
  89. </div>
  90. </div>
  91. <?php

    endwhile

    ;

    ?>
  92. <?php

    endwhile

    ;

    ?>
  93. <hr>
  94. <form id="comment-form" action="save_comment.php" method="POST">
  95. <div class="form-group">
  96. <label for="sender" class="control-label">Sender</label>
  97. <input type="text" name="sender" id="sender" class="form-control form-control-sm rounded-0" value="<?php

    echo

    isset

    (

    $_SESSION

    [

    'comment-post'

    ]

    )

    ? $_SESSION

    [

    'comment-post'

    ]

    [

    'sender'

    ]

    :

    ""

    ?>

    " required/>
  98. </div>
  99. <div class="form-group">
  100. <label for="comment" class="control-label">Comment</label>
  101. <textarea name="comment" id="comment" cols="30" rows="2" class="form-control" style="resize:none" placeholder="Write your comment here" required><?php

    echo

    isset

    (

    $_SESSION

    [

    'comment-post'

    ]

    )

    ? $_SESSION

    [

    'comment-post'

    ]

    [

    'comment'

    ]

    :

    ""

    ?>

    </textarea>
  102. </div>
  103. <div class="form-group">
  104. <div class="col-12 mt-4">
  105. <div class="d-flex justify-content-end align-items-end">
  106. <button class="btn btn-sm btn-sm btn-primary rounded-0 me-2" type="submit">Save</button>
  107. <button class="btn btn-sm btn-sm btn-secondary rounded-0" type="reset">Cancel</button>
  108. </div>
  109. </div>
  110. </div>
  111. </form>
  112. </div>
  113. </body>

  114. </html>

edit_comment.php
This file contains the script for the page where the users can edit the comment.

  1. <?php

    require_once

    (

    'Connection.php'

    )

    ?>
  2. <?php
  3. if

    (

    !

    isset

    (

    $_GET

    [

    'comment_id'

    ]

    )

    )

    {
  4. echo

    "<script>alert('Comment ID is required.'); location.replace('./');</script>"

    ;
  5. exit

    ;
  6. }
  7. ?>
  8. <!DOCTYPE html>
  9. <html lang="en">

  10. <head>
  11. <meta charset="UTF-8">
  12. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  13. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  14. <title>Simple Comment Section</title>
  15. <link rel="stylesheet" href="./css/bootstrap.min.css">
  16. <script src="./js/jquery-3.6.0.min.js"></script>
  17. <script src="./js/bootstrap.min.js"></script>
  18. <style>
  19. :root {
  20. --bs-success-rgb: 71, 222, 152 !important;
  21. }

  22. html,
  23. body {
  24. height: 100%;
  25. width: 100%;
  26. font-family: Apple Chancery, cursive;
  27. }
  28. </style>
  29. </head>

  30. <body class="bg-light">
  31. <nav class="navbar navbar-expand-lg navbar-dark bg-primary bg-gradient" id="topNavBar">
  32. <div class="container">
  33. <a class="navbar-brand" href="https://sourcecodester.com">
  34. Sourcecodester
  35. </a>
  36. </div>
  37. </nav>
  38. <div class="container py-3" id="page-container">
  39. <h3>Update Comment</h3>
  40. <?php

    if

    (

    isset

    (

    $_SESSION

    [

    'response_msg'

    ]

    )

    )

    :

    ?>
  41. <div class="alert alert-<?php

    echo

    $_SESSION

    [

    'response_type'

    ]

    ?>

    ">
  42. <p class="m-0"><?php

    echo

    $_SESSION

    [

    'response_msg'

    ]

    ?>

    </p>
  43. </div>
  44. <?php
  45. unset

    (

    $_SESSION

    [

    'response_msg'

    ]

    )

    ;
  46. unset

    (

    $_SESSION

    [

    'response_type'

    ]

    )

    ;
  47. endif

    ;
  48. ?>
  49. <hr>
  50. <?php
  51. $comment_qry

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `comments` where comment_id = '{$_GET['comment_id']}

    ' "

    )

    ;
  52. $result

    =

    $comment_qry

    ->

    fetchArray

    (

    )

    ;
  53. if

    (

    !

    $result

    )

    {
  54. echo

    "<script>alert('Unkown Comment ID.'); location.replace('./');</script>"

    ;
  55. exit

    ;
  56. }

    else

    {
  57. ?>
  58. <form id="comment-form" action="save_comment.php" method="POST">
  59. <input type="hidden" name="comment_id" value="<?php

    echo

    $_GET

    [

    'comment_id'

    ]

    ?>

    " >
  60. <div class="form-group">
  61. <label for="sender" class="control-label">Sender</label>
  62. <input type="text" name="sender" id="sender" class="form-control form-control-sm rounded-0" value="<?php

    echo

    isset

    (

    $_SESSION

    [

    'comment-post'

    ]

    )

    ? $_SESSION

    [

    'comment-post'

    ]

    [

    'sender'

    ]

    :

    (

    isset

    (

    $result

    [

    'sender'

    ]

    )

    ? $result

    [

    'sender'

    ]

    :

    ""

    )

    ?>

    " required/>
  63. </div>
  64. <div class="form-group">
  65. <label for="comment" class="control-label">Comment</label>
  66. <textarea name="comment" id="comment" cols="30" rows="2" class="form-control" style="resize:none" placeholder="Write your comment here" required><?php

    echo

    isset

    (

    $_SESSION

    [

    'comment-post'

    ]

    )

    ? $_SESSION

    [

    'comment-post'

    ]

    [

    'comment'

    ]

    :

    (

    isset

    (

    $result

    [

    'comment'

    ]

    )

    ? $result

    [

    'comment'

    ]

    :

    ""

    )

    ?>

    </textarea>
  67. </div>
  68. <div class="form-group">
  69. <div class="col-12 mt-4">
  70. <div class="d-flex justify-content-end align-items-end">
  71. <button class="btn btn-sm btn-sm btn-primary rounded-0 me-2" type="submit">Save</button>
  72. <a class="btn btn-sm btn-sm btn-secondary rounded-0" href="./">Cancel</a>
  73. </div>
  74. </div>
  75. </div>
  76. </form>
  77. <?php

    }

    ?>
  78. <hr>
  79. </div>

  80. </body>

  81. </html>

reply.php
This file contains the script for the page where the users can submit and edit the reply-to-comment.

  1. <?php

    require_once

    (

    'Connection.php'

    )

    ?>
  2. <?php
  3. if

    (

    !

    isset

    (

    $_GET

    [

    'comment_id'

    ]

    )

    )

    {
  4. echo

    "<script>alert('Comment ID is required.'); location.replace('./');</script>"

    ;
  5. exit

    ;
  6. }
  7. ?>
  8. <!DOCTYPE html>
  9. <html lang="en">

  10. <head>
  11. <meta charset="UTF-8">
  12. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  13. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  14. <title>Simple Comment Section</title>
  15. <link rel="stylesheet" href="./css/bootstrap.min.css">
  16. <script src="./js/jquery-3.6.0.min.js"></script>
  17. <script src="./js/bootstrap.min.js"></script>
  18. <style>
  19. :root {
  20. --bs-success-rgb: 71, 222, 152 !important;
  21. }

  22. html,
  23. body {
  24. height: 100%;
  25. width: 100%;
  26. font-family: Apple Chancery, cursive;
  27. }
  28. </style>
  29. </head>

  30. <body class="bg-light">
  31. <nav class="navbar navbar-expand-lg navbar-dark bg-primary bg-gradient" id="topNavBar">
  32. <div class="container">
  33. <a class="navbar-brand" href="https://sourcecodester.com">
  34. Sourcecodester
  35. </a>
  36. </div>
  37. </nav>
  38. <div class="container py-3" id="page-container">
  39. <h3>Reply to</h3>
  40. <?php

    if

    (

    isset

    (

    $_SESSION

    [

    'response_msg'

    ]

    )

    )

    :

    ?>
  41. <div class="alert alert-<?php

    echo

    $_SESSION

    [

    'response_type'

    ]

    ?>

    ">
  42. <p class="m-0"><?php

    echo

    $_SESSION

    [

    'response_msg'

    ]

    ?>

    </p>
  43. </div>
  44. <?php
  45. unset

    (

    $_SESSION

    [

    'response_msg'

    ]

    )

    ;
  46. unset

    (

    $_SESSION

    [

    'response_type'

    ]

    )

    ;
  47. endif

    ;
  48. ?>
  49. <hr>
  50. <?php
  51. $comment_qry

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `comments` where comment_id = '{$_GET['comment_id']}

    ' "

    )

    ;
  52. $result

    =

    $comment_qry

    ->

    fetchArray

    (

    )

    ;
  53. if

    (

    !

    $result

    )

    {
  54. echo

    "<script>alert('Unkown Comment ID.'); location.replace('./');</script>"

    ;
  55. exit

    ;
  56. }

    else

    {
  57. ?>
  58. <div class="card mb-2">
  59. <div class="card-body">
  60. <div class="d-flex align-items-end">
  61. <div class="fw-bold flex-grow-1"><?php

    echo

    ucwords

    (

    $result

    [

    'sender'

    ]

    )

    ?>

    </div>
  62. <span><small class="text-muted"><?php

    echo

    date

    (

    "m d,Y h:i A"

    ,

    strtotime

    (

    $result

    [

    'date_created'

    ]

    )

    )

    ?>

    </small></span>
  63. </div>
  64. <hr>
  65. <div class="lh-1">
  66. <p class=""><span class="mx-3"></span><?php

    echo

    str_replace

    (

    '\n'

    ,

    '<br/>'

    ,

    $result

    [

    'comment'

    ]

    )

    ?>

    </p>
  67. </div>
  68. </div>
  69. </div>
  70. <?php

    }

    ?>
  71. <hr>
  72. <?php
  73. if

    (

    isset

    (

    $_GET

    [

    'reply_id'

    ]

    )

    )

    {
  74. $reply_qry

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `replies` where reply_id = '{$_GET['reply_id']}

    ' "

    )

    ;
  75. $result2

    =

    $reply_qry

    ->

    fetchArray

    (

    )

    ;
  76. if

    (

    !

    $result2

    )

    {
  77. echo

    "<script>alert('Unkown Reply ID.'); location.replace('./');</script>"

    ;
  78. exit

    ;
  79. }
  80. }
  81. ?>
  82. <form id="comment-form" action="save_reply.php" method="POST">
  83. <input type="hidden" name="reply_id" value="<?php

    echo

    isset

    (

    $_GET

    [

    'reply_id'

    ]

    )

    ? $_GET

    [

    'reply_id'

    ]

    :

    ''

    ?>

    " >
  84. <input type="hidden" name="comment_id" value="<?php

    echo

    $_GET

    [

    'comment_id'

    ]

    ?>

    " >
  85. <div class="form-group">
  86. <label for="sender" class="control-label">Sender</label>
  87. <input type="text" name="sender" id="sender" class="form-control form-control-sm rounded-0" value="<?php

    echo

    isset

    (

    $_SESSION

    [

    'reply-post'

    ]

    )

    ? $_SESSION

    [

    'reply-post'

    ]

    [

    'sender'

    ]

    :

    (

    isset

    (

    $result2

    [

    'sender'

    ]

    )

    ? $result2

    [

    'sender'

    ]

    :

    ""

    )

    ?>

    " required/>
  88. </div>
  89. <div class="form-group">
  90. <label for="reply" class="control-label">Reply</label>
  91. <textarea name="reply" id="reply" cols="30" rows="2" class="form-control" style="resize:none" placeholder="Write your reply here" required><?php

    echo

    isset

    (

    $_SESSION

    [

    'reply-post'

    ]

    )

    ? $_SESSION

    [

    'reply-post'

    ]

    [

    'reply'

    ]

    :

    (

    isset

    (

    $result2

    [

    'reply'

    ]

    )

    ? $result2

    [

    'reply'

    ]

    :

    ""

    )

    ?>

    </textarea>
  92. </div>
  93. <div class="form-group">
  94. <div class="col-12 mt-4">
  95. <div class="d-flex justify-content-end align-items-end">
  96. <button class="btn btn-sm btn-sm btn-primary rounded-0 me-2" type="submit">Save</button>
  97. <a class="btn btn-sm btn-sm btn-secondary rounded-0" href="./">Cancel</a>
  98. </div>
  99. </div>
  100. </div>
  101. </form>
  102. </div>

  103. </body>

  104. </html>

Creating the PHP Action Scripts

The following PHP Scripts are the files that manages the queries on the database. Theese are the saving,inserting, and deleting scripts for comments and replies. Save the following files according to the given filename above each file scripts.

save_comment.php
The PHP Code that insert new comment or update old comment in the database.

  1. <?php
  2. require_once

    (

    'Connection.php'

    )

    ;
  3. if

    (

    isset

    (

    $_SESSION

    [

    'comment-post'

    ]

    )

    )

    {
  4. unset

    (

    $_SESSION

    [

    'comment-post'

    ]

    )

    ;
  5. }
  6. $comment_id

    =

    isset

    (

    $_POST

    [

    'comment_id'

    ]

    )

    ? $_POST

    [

    'comment_id'

    ]

    :

    ''

    ;
  7. $sender

    =

    $_POST

    [

    'sender'

    ]

    ;
  8. $comment

    =

    $conn

    ->

    escapeString

    (

    $_POST

    [

    'comment'

    ]

    )

    ;

  9. if

    (

    empty

    (

    $comment_id

    )

    )
  10. $sql

    =

    "INSERT INTO `comments` (`sender`,`comment`) VALUES ('{$sender}

    ','{$comment}

    ')"

    ;
  11. else
  12. $sql

    =

    "UPDATE `comments` set `sender` = '{$sender}

    ' ,`comment` = '{$comment}

    ' where comment_id = '$comment_id

    '"

    ;

  13. $save

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  14. if

    (

    $save

    )

    {
  15. $_SESSION

    [

    'response_type'

    ]

    =

    'success'

    ;
  16. $_SESSION

    [

    'response_msg'

    ]

    =

    'Comment Successfully saved.'

    ;
  17. if

    (

    empty

    (

    $comment_id

    )

    )
  18. header

    (

    'location:'

    .

    $_SERVER

    [

    'HTTP_REFERER'

    ]

    )

    ;
  19. else
  20. header

    (

    'location:./'

    )

    ;
  21. }

    else

    {
  22. $_SESSION

    [

    'response_type'

    ]

    =

    'danger'

    ;
  23. $_SESSION

    [

    'response_msg'

    ]

    =

    'Saving comment failed. Error: '

    .

    $conn

    ->

    lastErrorMsg

    (

    )

    ;
  24. $_SESSION

    [

    'comment-post'

    ]

    =

    $_POST

    ;
  25. header

    (

    'location:'

    .

    $_SERVER

    [

    'HTTP_REFERER'

    ]

    )

    ;
  26. }

save_reply.php
The PHP Code that insert new reply or update old reply in the database.

  1. <?php
  2. require_once

    (

    'Connection.php'

    )

    ;
  3. if

    (

    isset

    (

    $_SESSION

    [

    'reply-post'

    ]

    )

    )

    {
  4. unset

    (

    $_SESSION

    [

    'reply-post'

    ]

    )

    ;
  5. }
  6. $reply_id

    =

    isset

    (

    $_POST

    [

    'reply_id'

    ]

    )

    ? $_POST

    [

    'reply_id'

    ]

    :

    ''

    ;
  7. $comment_id

    =

    $_POST

    [

    'comment_id'

    ]

    ;
  8. $sender

    =

    $_POST

    [

    'sender'

    ]

    ;
  9. $reply

    =

    $conn

    ->

    escapeString

    (

    $_POST

    [

    'reply'

    ]

    )

    ;

  10. if

    (

    empty

    (

    $reply_id

    )

    )
  11. $sql

    =

    "INSERT INTO `replies` (`comment_id`,`sender`,`reply`) VALUES ('{$comment_id}

    ','{$sender}

    ','{$reply}

    ')"

    ;
  12. else
  13. $sql

    =

    "UPDATE `replies` set `sender` = '{$sender}

    ' ,`reply` = '{$reply}

    ' where reply_id = '$reply_id

    '"

    ;

  14. $save

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  15. if

    (

    $save

    )

    {
  16. $_SESSION

    [

    'response_type'

    ]

    =

    'success'

    ;
  17. $_SESSION

    [

    'response_msg'

    ]

    =

    'Reply Successfully saved.'

    ;
  18. header

    (

    'location:./'

    )

    ;
  19. }

    else

    {
  20. $_SESSION

    [

    'response_type'

    ]

    =

    'danger'

    ;
  21. $_SESSION

    [

    'response_msg'

    ]

    =

    'Saving reply failed. Error: '

    .

    $conn

    ->

    lastErrorMsg

    (

    )

    ;
  22. $_SESSION

    [

    'reply-post'

    ]

    =

    $_POST

    ;
  23. header

    (

    'location:'

    .

    $_SERVER

    [

    'HTTP_REFERER'

    ]

    )

    ;
  24. }

delete_comment.php
The PHP code for deleting comment.

  1. <?php
  2. require_once

    (

    'Connection.php'

    )

    ;
  3. $sql

    =

    "DELETE FROM `comments` where comment_id = '{$_GET['comment_id']}

    '"

    ;
  4. $delete

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  5. if

    (

    $delete

    )

    {
  6. $_SESSION

    [

    'response_type'

    ]

    =

    'success'

    ;
  7. $_SESSION

    [

    'response_msg'

    ]

    =

    'Comment Successfully deleted.'

    ;
  8. if

    (

    empty

    (

    $_GET

    [

    'comment_id'

    ]

    )

    )
  9. header

    (

    'location:'

    .

    $_SERVER

    [

    'HTTP_REFERER'

    ]

    )

    ;
  10. else
  11. header

    (

    'location:./'

    )

    ;
  12. }

    else

    {
  13. $_SESSION

    [

    'response_type'

    ]

    =

    'danger'

    ;
  14. $_SESSION

    [

    'response_msg'

    ]

    =

    'Deleting comment failed. Error: '

    .

    $conn

    ->

    lastErrorMsg

    (

    )

    ;
  15. $_SESSION

    [

    'comment-post'

    ]

    =

    $_POST

    ;
  16. header

    (

    'location:'

    .

    $_SERVER

    [

    'HTTP_REFERER'

    ]

    )

    ;
  17. }
  18. ?>

delete_reply.php
The PHP code for deleting reply.

  1. <?php
  2. require_once

    (

    'Connection.php'

    )

    ;
  3. $sql

    =

    "DELETE FROM `replies` where reply_id = '{$_GET['reply_id']}

    '"

    ;
  4. $delete

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  5. if

    (

    $delete

    )

    {
  6. $_SESSION

    [

    'response_type'

    ]

    =

    'success'

    ;
  7. $_SESSION

    [

    'response_msg'

    ]

    =

    'Reply Successfully deleted.'

    ;
  8. if

    (

    empty

    (

    $_GET

    [

    'reply_id'

    ]

    )

    )
  9. header

    (

    'location:'

    .

    $_SERVER

    [

    'HTTP_REFERER'

    ]

    )

    ;
  10. else
  11. header

    (

    'location:./'

    )

    ;
  12. }

    else

    {
  13. $_SESSION

    [

    'response_type'

    ]

    =

    'danger'

    ;
  14. $_SESSION

    [

    'response_msg'

    ]

    =

    'Deleting reply failed. Error: '

    .

    $conn

    ->

    lastErrorMsg

    (

    )

    ;
  15. $_SESSION

    [

    'reply-post'

    ]

    =

    $_POST

    ;
  16. header

    (

    'location:'

    .

    $_SERVER

    [

    'HTTP_REFERER'

    ]

    )

    ;
  17. }
  18. ?>

There you go. You can now test the source code on your end. If you have encountered any errors, kindly review your source code by differentiating it from the codes I provided above. You can also download the working source code I created for this tutorial. The download button is located below this article.

DEMO VIDEO

That's the end of this tutorial. I hope this Comment Section tutorial in PHP and SQLite will help you and useful for your PHP Projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

324,125

324,133

Top