~~~
<?php
if (!function_exists('json')) {
//输出JSON格式的结果集
function json($code, $message, $data = null)
{
$result = array(
"errno" => $code,
"errmsg" => $message,
"data" => $data,
);
$json = json_encode($result);
header('Content-Type:text/json');
echo $json;
}
}
//数据库连接部分--开始
$servername = "localhost"; //数据库服务器名称
$username = "root"; // 连接数据库用户名
$password = "root"; // 连接数据库密码
$database = "quickstart"; // 数据库的名字
// 创建连接
$conn = new mysqli($servername, $username, $password, $database);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
$action = isset($_GET['action']) ? $_GET['action'] : '';
if ($action == 'getBookList') {
$bookList = getBookList();
json(0, '', $bookList);
} else if ($action == 'getBookInfo') {
//从数据库中查询
$id = isset($_GET['id']) ? $_GET['id'] : null;
if ($id) {
$book = getBookInfo($id);
} else {
$book = null;
}
json(0, '', $book);
} else if ($action == 'updateBookInfo') {
$result = updateBookInfo();
json(0, '', $result);
} else if ($action == 'addBookInfo') {
$result = addBookInfo();
json(0, '', $result);
} else if ($action == 'deleteBookInfo') {
$id = isset($_POST['id']) ? $_POST['id'] : null;
$result = deleteBookInfo($id);
json(0, '', $result);
} else {
json(1000, '错误的请求');
}
$conn->close(); //关闭连接
/*--结束*/
/**
* 获取图书列表
*/
function getBookList()
{
global $conn;
$sql = "select * from think_book";
$result = $conn->query($sql);
$data = [];
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
}
return $data;
}
/**
* 获取图书信息
*/
function getBookInfo($id)
{
global $conn;
$sql = "select * from think_book where id=" . $id;
$result = $conn->query($sql);
$data = null;
if ($result->num_rows > 0) {
$data = $result->fetch_assoc();
}
return $data;
}
function updateBookInfo()
{
global $conn;
// 预处理及绑定
$stmt = $conn->prepare("update think_book set title=?, author=?, publisher=?, pub_year=?, price=? where id=?");
$stmt->bind_param("ssssds", $title, $author, $publisher, $pub_year, $price, $id);
// 设置参数并执行
$id = isset($_POST['id']) ? $_POST['id'] : '';
$title = isset($_POST['title']) ? $_POST['title'] : '';
$author = isset($_POST['author']) ? $_POST['author'] : '';
$publisher = isset($_POST['publisher']) ? $_POST['publisher'] : '';
$pub_year = isset($_POST['pub_year']) ? $_POST['pub_year'] : null;
$price = isset($_POST['price']) ? $_POST['price'] : null;
$stmt->execute();
$stmt->close();
return array(
'id' => $id,
'title' => $title,
'author' => $author,
'publisher' => $publisher,
'pub_year' => $pub_year,
'price' => $price
);
}
function deleteBookInfo($id)
{
global $conn;
$sql = "DELETE from think_book where id=" . $id;
$result = $conn->query($sql);
return $result;
}
function addBookInfo()
{
global $conn;
// 预处理及绑定
$stmt = $conn->prepare("INSERT INTO think_book(title, author, publisher, pub_year, price) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("ssssd", $title, $author, $publisher, $pub_year, $price);
// 设置参数并执行
$title = isset($_POST['title']) ? $_POST['title'] : '';
$author = isset($_POST['author']) ? $_POST['author'] : '';
$publisher = isset($_POST['publisher']) ? $_POST['publisher'] : '';
$pub_year = isset($_POST['pub_year']) ? $_POST['pub_year'] : null;
$price = isset($_POST['price']) ? $_POST['price'] : null;
$stmt->execute();
$stmt->close();
return $conn->insert_id; //返回新增记录的ID
}
~~~