search
HomeBackend DevelopmentPHP TutorialDetailed explanation of how to implement comment reply and delete function in PHP

How to implement comment reply and delete function in PHP? This article mainly introduces the comment reply and delete function in PHP in detail. It has certain reference value. Interested friends can refer to it. I hope to be helpful.

Simple comment reply and delete function, the specific content is as follows

1. Database

Create two tables, one is the pinglun table; the other is huifu Table

## The effect is as follows:

The code is as follows:

1. Main page main.php

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<h1 id="朋友圈">朋友圈</h1>
<p>内容:</p>
<p>今天很嗨</p>
<p><img  src="/static/imghwm/default1.png"  data-src="../picture/timg.jpg"  class="lazy"      style="max-width:90%"  style="max-width:90%" alt="Detailed explanation of how to implement comment reply and delete function in PHP" ></p><br>
<form action="mainchuli.php" method="post">
    <input type="text" hidden="hidden" value="zhangsan" name="zhangsan"> <!--因为没有权限,这里给了一个默认值-->
    <textarea name="content"></textarea><input type="submit" value="评论"><!--评论显示的地方--><!--单击评论提交内容进处理页面-->
</form>
 
<!--?php
  require"DBDA.class.php"; //调用封装类注意修改数据库名
  $db = new DBDA();
  $sql ="select * from Pinglun";
  $arr = $db--->query($sql,1);
  foreach($arr as $v)
  {
    echo"
       <p style="color:blue">{$v[1]} {$v[3]}</p>
       <p style="color:blue">{$v[2]}</p>
       <form action="delchuli.php?id={$v[0]}" method="post"> //删除按钮
        <input type="submit" value="删除">
       </form>
       <form action="huifuchuli.php?id={$v[0]}" method="post"> //回复按钮
        <textarea name="Comment"></textarea><input type="submit" value="回复">
       </form>
       ";
       
      $dc = new DBDA(); 
      $sql1 ="select * from huifu where jieshouid ={$v[0]}"; //查询回复表中的id和传过去的id是不是一样的
          $arr1 = $dc->query($sql1,1);
      foreach($arr1 as $k)
      {
        echo "<p>{$k[2]} {$k[3]}</p>
           <p>{$k[4]}</p>
           ";
      }
         
  }   
?>

2. Comment processing page pinglunchuli.php

<?php
$zhangsan = $_POST["zhangsan"];
$content = $_POST["content"];
$time = date("Y-m-d H:i:s");
 
require "DBDA.class.php";
$db = new DBDA();
$sql = "insert into Pinglun values(&#39;&#39;,&#39;{$zhangsan}&#39;,&#39;{$content}&#39;,&#39;{$time}&#39;)";
$db->query($sql);
header("location:main.php");

3. Reply processing page huifuchuli.php

<!--?php
$id = $_GET["id"]; //将点击回复的评论id传过来
$Comment = $_POST["Comment"]; //回复文本域中的内容
$me = "me"; //这里是给定义了一个人
$Times = date("Y-m-d H:i:s");
 
require "DBDA.class.php";
$db = new DBDA();
$sql = "insert into huifu values(&#39;&#39;,&#39;{$id}&#39;, &#39;{$me}&#39;,&#39;{$Times}&#39;,&#39;{$Comment}&#39;)";
$db--->query($sql);
header("location:main.php");

4. Delete processing Page delchuli.php

<?php
$id = $_GET["id"];
require "DBDA.class.php";
$db = new DBDA();
$sql = "delete from Pinglun where id=&#39;{$id}&#39;";
if($db->query($sql))
{
 
  header("location:main.php");
}
else
{
  echo "删除失败!";
}

Related recommendations:

thinkphp5 method of uploading images and generating thumbnails

Detailed explanation of how PHP implements a simple search box automatic prompt function

Detailed explanation of how PHP obtains the audio file duration

The above is the detailed content of Detailed explanation of how to implement comment reply and delete function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What data can be stored in a PHP session?What data can be stored in a PHP session?May 02, 2025 am 12:17 AM

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

How do you start a PHP session?How do you start a PHP session?May 02, 2025 am 12:16 AM

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

What is session regeneration, and how does it improve security?What is session regeneration, and how does it improve security?May 02, 2025 am 12:15 AM

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

What are some performance considerations when using PHP sessions?What are some performance considerations when using PHP sessions?May 02, 2025 am 12:11 AM

PHP sessions have a significant impact on application performance. Optimization methods include: 1. Use a database to store session data to improve response speed; 2. Reduce the use of session data and only store necessary information; 3. Use a non-blocking session processor to improve concurrency capabilities; 4. Adjust the session expiration time to balance user experience and server burden; 5. Use persistent sessions to reduce the number of data read and write times.

How do PHP sessions differ from cookies?How do PHP sessions differ from cookies?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

How does PHP identify a user's session?How does PHP identify a user's session?May 01, 2025 am 12:23 AM

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

What are some best practices for securing PHP sessions?What are some best practices for securing PHP sessions?May 01, 2025 am 12:22 AM

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version