在 admin 目录中添加一个 options.php 文件,处理获取和更新选项业务:
xxxxxxxxxx661<?php2/**3 * 获取或更新配置选项4 */56require '../functions.php';78// 设置响应类型为 JSON9header('Content-Type: application/json');1011// 如果是 GET 请求,则获取指定配置12if ($_SERVER['REQUEST_METHOD'] == 'GET') {13 if (empty($_GET['key'])) {14 // echo json_encode(array(15 // 'success' => false,16 // 'message' => 'option key required'17 // ));18 // exit; // 不再往下执行19 // 查询全部数据20 $data = xiu_query('select * from `options`');21 echo json_encode(array(22 'success' => true,23 'data' => $data24 ));25 exit; // 不再往下执行26 }27 // 查询数据28 $data = xiu_query(sprintf("select `value` from `options` where `key` = '%s' limit 1;", $_GET['key']));29 // 返回30 if (isset($data[0][0])) {31 echo json_encode(array(32 'success' => true,33 'data' => $data[0][0]34 ));35 } else {36 echo json_encode(array(37 'success' => false,38 'message' => 'option key does not exist'39 ));40 }41 exit; // 不再往下执行42}4344// 否则是更新或新增配置4546if (empty($_POST['key']) || empty($_POST['value'])) {47 // 关键数据不存在48 echo json_encode(array(49 'success' => false,50 'message' => 'option key and value required'51 ));52 exit; // 不再往下执行53}5455// 判断是否存在该属性56$exist = xiu_query(sprintf("select count(1) from `options` where `key` = '%s'", $_POST['key']))[0][0] > 0;5758if ($exist) {59 $affected_rows = xiu_execute(sprintf("update `options` set `value` = '%s' where `key` = '%s'", $_POST['value'], $_POST['key']));60} else {61 $affected_rows = xiu_execute(sprintf("insert into `options` values (null, '%s', '%s')", $_POST['key'], $_POST['value']));62}6364echo json_encode(array(65 'success' => $affected_rows > 066));🚩 源代码: step-75