代码拉取完成,页面将自动刷新
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2021/9/2
* Time: 12:35
*/
if(!session_id()) session_start();
header("Access-Control-Allow-Origin:*");
header('Access-Control-Allow-Methods:POST,GET');
header('Access-Control-Allow-Headers:x-requested-with,content-type');
require_once('dbServerConnect.php');
$con = new DB();
$link = $con->mySqlServer();
if (!$link) {
echo '数据库连接失败';
return;
}
// 判断请求类型
$method = $_SERVER['REQUEST_METHOD'];
if ($method === "POST") {
// 获取前端axios post 请求的json数据
$jsonData = file_get_contents("php://input");
// json_decode — 对 JSON 格式的字符串进行解码 true:输出为数组,不写则为对象
$decodeData = json_decode($jsonData, true);
if(isset($decodeData['commentForm']) && !empty($decodeData['commentForm']['blog_id']) && !empty($decodeData['commentForm']['comment']) && !empty($decodeData['commentForm']['topic_title'])) {
// $commentForm = $decodeData['commentForm'];
if(!empty($decodeData['commentForm']['username'])){
$username = $decodeData['commentForm']['username'];
}
else {
$username = "游客";
}
$blog_id = $decodeData['commentForm']['blog_id'];
$osName = $decodeData['commentForm']['osName'];
$osVersion = $decodeData['commentForm']['osVersion'];
$topic_title = $decodeData['commentForm']['topic_title'];
$comment = str_replace("'", "''",$decodeData['commentForm']['comment']);
$sql = "insert into comment(blog_container_id,username,comment,createdate,osName,osVersion,topic_title) values ($blog_id,'$username','$comment',now(),'$osName','$osVersion','$topic_title')";
$executeSql = mysqli_query($link,$sql) or die(mysqli_error($link));
// 添加评论成功后查询评论操作
if($executeSql) {
$sql = "select * from comment where blog_container_id=$blog_id";
$result = mysqli_query($link,$sql);
//将查询的结果集封装到一个数组里
$res = mysqli_fetch_all($result);
$arrayLent = sizeof($res);//获取评论数组长度
// 每次取出最新一条评论
$getData = $arrayLent - 1;
$sql = "select * from comment where blog_container_id=$blog_id limit $getData, $arrayLent";
$result = mysqli_query($link,$sql);
//将查询的结果集封装到一个数组里
$res = mysqli_fetch_all($result);
$res = array('result' => array('status' => 1,'mgs'=>'评论成功!','commentList'=>$res));
echo json_encode($res);
return;
}
echo $executeSql;
}
else {
$res = array('result' => array('status' => 0,'mgs'=>'添加参数错误!'));
echo json_encode($res);
}
}
//获取所有评论
elseif ($method === "GET") {
$blog_id = $_GET['blog_id'];
$sql = "select * from comment where blog_container_id=$blog_id order by createdate desc";
$result = mysqli_query($link,$sql);
//将查询的结果集封装到一个数组里
$res = mysqli_fetch_all($result);
if($res) {
$res = array('result' => array('status' => 1,'mgs'=>'获取评论成功!','commentList'=>$res));
echo json_encode($res);
}
else {
$res = array('result' => array('status' => 0,'mgs'=>'获取评论失败!'));
echo json_encode($res);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。