]; 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 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 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('','{$yonghu}','{$content}','{$time}')"; 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='huifuchuli.php?id={$v[0]}' method='post'> <input type='text' name='Comment' /> <input type='submit' value='回复' /></form>"; $dc = new DBDA(); $sql1="select * from huifu where jieshouid={$v[0]}"; $arr1=$dc->query($sql1); foreach($arr1 as $f) { echo "<div style='color:red'>{$f[0]}</div> <div style='color:red'>{$f[2]}</div> <div style='color:red'>{$f[3]}</div> <div style='color:red'>{$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='shanchuchuli.php?id={$v[0]}' method='post'> <input type='submit' value='删除' /></form> <form action='huifuchuli.php?id={$v[0]}' method='post'> <input type='text' name='Comment' /> <input type='submit' value='回复' /></form>";
Processing page
<?php $id = $_GET["id"]; require "DBDA.class.php"; $db=new DBDA(); $sql="delete from pinglun where id='{$id}'"; if($db->query($sql,0)) { header("location:pinglun.php"); } else { echo "不能删除!"; }
The delete button in the picture will appear
Let’s try the effect:
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!