php如何实现评论功能?

2020-08-12 20:37:24

php实现评论功能的方法:1、设置发布评论文本域,代码为【 type="submit" value="评论" />】;2、在评论一条后面跟着回复,代码为【type='submit' value='回复'】。

php实现评论功能的方法:

1.发布评论

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

这是评论的文本域

评论后的内容要存到数据库去处理

因为这只是要实现简单的评论与回复,所以没有设登录权限所以里面的名字都是添加上去的

<?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.回复功能这里是评论一条后面跟着回复

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

将评论的内容与回复的内容遍历出来显示即可

这样就可以实现一条评论后面跟着一条回复

3.再就是删除信息

就是在回复的前面加一个删除按钮

<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>";

处理页面

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

这样就出来图中的删除按钮 

我们试试效果:

c4824c1f1ea0b24083031f679e1eaf7.png

相关文章 大家在看