How to implement comment function in php?

coldplay.xixi
Release: 2023-03-03 07:30:01
Original
8507 people have browsed it

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>
Copy after login

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 "你输入错误!";
}
Copy after login

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>
             ";
    }
}
?>
Copy after login

. 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>";
Copy after login

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 "不能删除!";
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!