**目錄**
[TOC]
## 圖書更新接口
增加更新圖書信息的接口updateBookInfo
> 更新接口使用了預處理語句提高代碼的可讀性,具體請閱讀Runoob網站相關的介紹
完整的代碼列表:
~~~
<?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 {
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
);
}
~~~
## 測試接口
設置請求的數據格式為`Content-Type = application/x-www-form-urlencoded`

模擬發起Form的表單請求,填寫相應的參數

接口返回更新后的數據
~~~
{
"errno": 0,
"errmsg": "",
"data": {
"id": "1",
"title": "ThinkPHP API開發指南",
"author": "曾青松",
"publisher": "清華大學出版社",
"pub_year": "2020",
"price": "100.50"
}
}
~~~
## 練習
1. 模擬發送JSON格式的數據
> 在做接口調用的時候更多的是以JSON格式發起請求
問題分析:
設置請求的數據格式為`Content-Type = application/json`
在body部分輸入JSON格式的數據,并發送:

接收數據的代碼需要更改如下:
~~~
$json_raw = file_get_contents("php://input");
$json_data = json_decode($json_raw);
~~~
請寫出接收并處理數據的完整函數代碼。
2. 編寫實現新增圖書的接口`addBookInfo`和刪除圖書`deleteBookInfo`的接口
~~~
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
}
~~~
## 擴展閱讀
1. mysqli擴展庫的使用
> 掌握mysqli數據庫的增刪改查操作的實現
2. 關聯數組的使用
3. 掌握JSON數據格式