ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
**目录** [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` ![](https://img.kancloud.cn/b1/d2/b1d222a83f2b7178d22835a632850b5a_829x763.png) 模拟发起Form的表单请求,填写相应的参数 ![](https://img.kancloud.cn/9d/af/9daf3f9d91883c6198b9ebd812c61ca8_813x714.png) 接口返回更新后的数据 ~~~ { "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格式的数据,并发送: ![](https://img.kancloud.cn/00/7d/007d5711f048476da7828325c201256c_827x674.png) 接收数据的代码需要更改如下: ~~~ $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数据格式