1 Star 15 Fork 7

chenxuyun/vue_blog_server

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
addComment.php 3.41 KB
一键复制 编辑 原始数据 按行查看 历史
chenxuyun 提交于 2021-09-05 10:48 . vue_blog网站后台
<?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);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/chenxuyun/vue_blog_server.git
git@gitee.com:chenxuyun/vue_blog_server.git
chenxuyun
vue_blog_server
vue_blog_server
master

搜索帮助