<?php
@header("Content-Type:text/html;charset=utf-8");
const host = "localhost:3306";
const user = "root";
const pwd = "root";
const dbName = "2101";
$conn = mysqli_connect(host,user,pwd,dbName);
if(!$conn){
exit("数据库链接失败");
}
mysqli_query($conn,"set name utf8");
mysqli_query($conn,"set character set utf-8");
<?php
@header("Content-Type:text/html;charset=utf-8");
function err(){
$msg = array();
$msg["status"] = false;
$msg["detail"] = "请传入完整参数";
exit(json_encode($msg));
}
function isRight($key){
global $conn;
$val = $_GET[$key];
if(!$val){
err();
}
$search = "select * from `userinfo` where $key = '$val'";
$result = mysqli_query($conn, $search);
$item = mysqli_fetch_assoc($result);
$list = array("user" => "用户名","phone"=>"电话","email"=>"邮箱");
$msg = array();
if(!$item){
$msg["status"] = true;
$msg["detail"] = "可以使用的".$list[$key];
}else{
$msg["status"] = false;
$msg["detail"] = $list[$key]."已注册";
}
echo json_encode($msg);
}
<?php
@include_once("conn.php");
@include_once("isRight.php");
if(isset($_GET["user"])){
isRight("user");
}else if(isset($_GET["phone"])){
isRight("phone");
}else if(isset($_GET["email"])){
isRight("email");
}else{
err();
}
<?php
@include_once("conn.php");
@include_once("isRight.php");
$user = $_GET["user"];
$pwd = $_GET["pwd"];
$phone = $_GET["phone"];
$email = $_GET["email"];
if(!($user&&$pwd&&$phone&&$email)){
err();
}
$insert = "insert into `userinfo`(user,pwd,phone,email) values('$user','$pwd','$phone','$email')";
mysqli_query($conn,$insert);
$rows = mysqli_affected_rows($conn);
$msg = array();
if($rows >0){
$msg["status"] = true;
$msg["detail"] = "新增成功";
}else{
$msg["status"] = false;
$msg["detail"] = "新增失败";
$msg["sql"] = $insert;
}
echo json_encode($msg);
<?php
@include_once("conn.php");
@include_once("isRight.php");
$account = $_POST["account"];
$pwd = $_POST["pwd"];
if(!($account&&$pwd)){
err();
}
$search = "select * from `userinfo` where user = '$account' or phone = '$account' or email = '$account'";
$result = mysqli_query($conn, $search);
$item = mysqli_fetch_assoc($result);
$msg = array();
if($item){
$realPwd = $item["pwd"];
if($realPwd === $pwd){
$msg["status"] = true;
$msg["detail"] = "登录成功";
$msg["user"] = $item["user"];
}else{
$msg["status"] = false;
$msg["detail"] = "密码有误";
}
}else{
$msg["status"] = false;
$msg["detail"] = "该用户未注册";
}
echo json_encode($msg);
<?php
@include_once("conn.php");
@include_once("isRight.php");
$id = $_GET["id"];
if(!$id){
err();
}
$del = "delete from `grade` where id in ('$id')";
mysqli_query($conn,$del);
$rows = mysqli_affected_rows($conn);
$msg = array();
if($rows >0){
$msg["status"] = true;
$msg["detail"] = "删除成功";
}else if($rows == 0){
$msg["status"] = false;
$msg["detail"] = "该数据已被删除";
}else{
$msg["status"] = false;
$msg["detail"] = "删除失败,语法有误";
$msg["sql"] = $del;
}
echo json_encode($msg);
<?php
@include_once("conn.php");
@include_once("isRight.php");
$id = $_GET["id"];
if(!$id){
err();
}
$search = "SELECT id,NAME,class,chinese,math,english,chinese+math+english AS total FROM `grade` where id='$id'";
$result = mysqli_query($conn, $search);
$item = mysqli_fetch_assoc($result);
$msg = array();
if($item){
$msg["status"] = true;
$msg["detail"] = "查询成功";
$msg["data"] = $item;
}else{
$msg["status"] = false;
$msg["detail"] = "查询失败";
$msg["data"] = null;
}
echo json_encode($msg);
<?php
@include_once("conn.php");
@include_once("isRight.php");
$id = $_GET["id"];
if(!$id){
err();
}
$search = "SELECT id,NAME,class,chinese,math,english,chinese+math+english AS total FROM `grade` where id='$id'";
$result = mysqli_query($conn, $search);
$item = mysqli_fetch_assoc($result);
$msg = array();
if($item){
$msg["status"] = true;
$msg["detail"] = "查询成功";
$msg["data"] = $item;
}else{
$msg["status"] = false;
$msg["detail"] = "查询失败";
$msg["data"] = null;
}
echo json_encode($msg);
<?php
@include_once("conn.php");
@include_once("isRight.php");
$key = isset($_GET["key"]) ? $_GET["key"] : "";
$orderCol = isset($_GET["orderCol"]) ? $_GET["orderCol"] : "id";
$orderType = isset($_GET["orderType"]) ? $_GET["orderType"] : "asc";
$search = "select id,NAME,class,chinese,math,english,chinese+math+english as total from `grade` where NAME like '%$key%' order by $orderCol $orderType";
$result = mysqli_query($conn, $search);
$list = array();
while ($item = mysqli_fetch_assoc($result)) {
array_push($list, $item);
}
echo json_encode($list);
<?php
@include_once("conn.php");
@include_once("isRight.php");
$key = $_GET["key"];
$orderCol = $_GET["orderCol"];
$orderType = $_GET["orderType"];
$pageIndex = $_GET["pageIndex"];
$showNum = $_GET["showNum"];
$searchAll = "select count(*) as count from `grade` where NAME like '%$key%'";
$result = mysqli_query($conn, $searchAll);
$item = mysqli_fetch_assoc($result);
$count = $item["count"] * 1;
$maxPage = ceil($count / $showNum);
if ($pageIndex > $maxPage) {
$pageIndex = $maxPage;
}
if ($pageIndex < 1) {
$pageIndex = 1;
}
$skipNum = ($pageIndex - 1) * $showNum;
$search = "select id,NAME,class,chinese,math,english,chinese+math+english as total from `grade` where NAME like '%$key%' order by $orderCol $orderType limit $skipNum,$showNum";
$res = mysqli_query($conn, $search);
$msg = array();
while ($item = mysqli_fetch_assoc($res)) {
array_push($msg, $item);
}
$obj = array();
if ($msg) {
$obj["status"] = true;
$obj["detail"] = "查询成功";
$obj["maxPage"] = $maxPage;
$obj["count"] = $count;
$obj["list"] = $msg;
}else{
$obj["status"] = false;
$obj["detail"] = "查询失败";
$obj["sql"] = $search;
}
echo json_encode($obj);