]; 2. Follow a comment with a reply, the code is [type='submit' value ='reply']."/> ]; 2. Follow a comment with a reply, the code is [type='submit' value ='reply'].">

Home  >  Article  >  Backend Development  >  How to implement comment function in php?

How to implement comment function in php?

coldplay.xixi
coldplay.xixiOriginal
2020-07-23 16:10:168520browse

How to implement the comment function in php: 1. Set the post comment text field, the code is [ type="submit" value="Comment" />]; 2. Follow a comment with a reply, the code is [type='submit' value='reply'].

How to implement comment function in php?

How to implement the comment function in php:

1. Post a comment

<form action="pinglunchili.php" method="post">
<textarea name="content"></textarea>
<div><input type="submit" value="评论" /></div>
 
</form>

This is the text field of the comment

The content after the comment must be stored in the database for processing

Because this is just to implement simple comments and replies, there is no login permission, so there is The names are all added

<?php
 
$yonghu="caocao";
 
$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;{$yonghu}&#39;,&#39;{$content}&#39;,&#39;{$time}&#39;)";
 
if($db->query($sql,0))
{
    header("location:pinglun.php");
}
else
{
    echo "你输入错误!";
}

2. The reply function here is a comment followed by a reply

<?php
 
require "DBDA.class.php";
$db=new DBDA();
$sql="select * from pinglun";
$arr=$db->query($sql);
 
foreach($arr as $v)
{
     
    echo "<div>{$v[0]}</div>
          <div>{$v[1]}</div>
              <div>{$v[2]}</div>
          <div>{$v[3]}</div>
          <form action=&#39;huifuchuli.php?id={$v[0]}&#39; method=&#39;post&#39;>
          <input type=&#39;text&#39; name=&#39;Comment&#39; />
                  <input type=&#39;submit&#39; value=&#39;回复&#39; /></form>";
    $dc = new DBDA();    
    $sql1="select * from huifu where jieshouid={$v[0]}";
    $arr1=$dc->query($sql1);
    foreach($arr1 as $f)
    {
        echo "<div style=&#39;color:red&#39;>{$f[0]}</div>
              <div style=&#39;color:red&#39;>{$f[2]}</div>
              <div style=&#39;color:red&#39;>{$f[3]}</div>
              <div style=&#39;color:red&#39;>{$f[4]}</div>
             ";
    }
}
?>

. The content of the comment and the content of the reply will be traversed and displayed. But

In this way, a comment can be followed by a reply

3. Then to delete the message

is to add a delete button in front of the reply

<form action=&#39;shanchuchuli.php?id={$v[0]}&#39; method=&#39;post&#39;>
             <input type=&#39;submit&#39; value=&#39;删除&#39; /></form>
         <form action=&#39;huifuchuli.php?id={$v[0]}&#39; method=&#39;post&#39;>
         <input type=&#39;text&#39; name=&#39;Comment&#39; />
         <input type=&#39;submit&#39; value=&#39;回复&#39; /></form>";

Processing page

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

The delete button in the picture will appear

Let’s try the effect:

How to implement comment function in php?

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of How to implement comment 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