php $_POST传值

博客围绕PHP中$_POST传值展开,虽内容简略,但聚焦于该信息技术领域的关键操作,$_POST传值在PHP后端开发的数据交互中十分重要。
1、

2、



// 获取全部order接口:api.php?action=order_list // order表 CRUD if ($action === 'order_list') { $res = $conn->query('SELECT * FROM `order` ORDER BY id DESC'); $data = []; while ($row = $res->fetch_assoc()) { $data[] = $row; } echo json_encode(['code' => 200, 'message' => '接口调用成功', 'data' => $data]); exit; } if ($action === 'order_add') { // 兼容所有请求格式,增强调试和健壮性 $params = $_POST; $raw = file_get_contents('php://input'); if (empty($params) && !empty($raw)) { $json = json_decode($raw, true); if (json_last_error() === JSON_ERROR_NONE && is_array($json)) { $params = $json; } else { parse_str($raw, $params); } } // 日志记录原始和解析后参数 file_put_contents('debug.log', "Raw input: " . $raw . "\n", FILE_APPEND); file_put_contents('debug.log', "Parsed params: " . print_r($params, true) . "\n", FILE_APPEND); // 统一参数获取 $user_uid = isset($params['user_uid']) ? intval($params['user_uid']) : (isset($_GET['user_uid']) ? intval($_GET['user_uid']) : 0); $quantity = isset($params['quantity']) ? intval($params['quantity']) : (isset($_GET['quantity']) ? intval($_GET['quantity']) : 0); $status = isset($params['status']) ? $params['status'] : ($_GET['status'] ?? ''); $created_at = isset($params['created_at']) ? $params['created_at'] : ($_GET['created_at'] ?? ''); $order_no = isset($params['order_no']) ? $params['order_no'] : ($_GET['order_no'] ?? ''); // 严格参数校验 $requiredFields = ['user_uid', 'quantity', 'status', 'created_at', 'order_no']; foreach ($requiredFields as $field) { if (!isset($params[$field]) || $params[$field] === '' || $params[$field] === null) { file_put_contents('debug.log', "缺少必要参数: $field\n", FILE_APPEND); echo json_encode(['code' => 1, 'msg' => "缺少必要参数: $field"]); exit; } } $stmt = $conn->prepare('INSERT INTO `order` (user_uid, quantity, status, created_at, order_no) VALUES (?, ?, ?, ?, ?)'); $stmt->bind_param('iisss', $user_uid, $quantity, $status, $created_at, $order_no); $stmt->execute(); echo json_encode(['code' => 0, 'msg' => '添加成功', 'id' => $stmt->insert_id]); exit; } if ($action === 'order_edit') { $params = $_POST; if (empty($params)) { $raw = file_get_contents('php://input'); $json = json_decode($raw, true); if (json_last_error() === JSON_ERROR_NONE && is_array($json)) { $params = $json; } else { parse_str($raw, $params); } } $id = intval($params['id'] ?? $_GET['id'] ?? 0); $amount = floatval($params['amount'] ?? $_GET['amount'] ?? 0); $status = $params['status'] ?? $_GET['status'] ?? ''; if (!$id || !$amount || $status === '') { echo json_encode(['code' => 1, 'msg' => '参数缺失']); exit; } $stmt = $conn->prepare('UPDATE `order` SET amount=?, status=? WHERE id=?'); $stmt->bind_param('dsi', $amount, $status, $id); $stmt->execute(); echo json_encode(['code' => 0, 'msg' => '修改成功']); exit; } if ($action === 'order_delete') { $params = $_POST; if (empty($params)) { $raw = file_get_contents('php://input'); $json = json_decode($raw, true); if (json_last_error() === JSON_ERROR_NONE && is_array($json)) { $params = $json; } else { parse_str($raw, $params); } } $id = intval($params['id'] ?? $_GET['id'] ?? 0); if (!$id) { echo json_encode(['code' => 1, 'msg' => '参数缺失']); exit; } $conn->query("DELETE FROM `order` WHERE id=$id"); echo json_encode(['code' => 0, 'msg' => '删除成功']); exit; }后报无user_uid参数,但是是写了的
最新发布
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值