曹え 5811 发布于:2025-06-13 03:49:49
// 数据采集表单 function updata(){ $this->display($this->template.'/updata'); } function updatasave(){ // 如果是POST请求,处理保存数据 if ($_SERVER['REQUEST_METHOD'] === 'POST') { // 获取原始POST数据 $json = file_get_contents('php://input'); $data = json_decode($json, true); if ($data === null) { // 记录错误日志 file_put_contents('post_error.log', $json . "\n", FILE_APPEND); echo json_encode(['code' => 0, 'message' => '无效的JSON数据']); exit; } // 验证必要字段 if (empty($data['title'])) { echo json_encode(['code' => 0, 'message' => '标题不能为空']); exit; } // 处理描述字段,去除HTML标签 if (!empty($data['description'])) { $data['description'] = strip_tags($data['description']); } if (!empty($data['descs'])) { $data['descs'] = strip_tags($data['descs']); } // 尝试保存数据 try { $result = M('Article')->add($data); if ($result) { echo json_encode(['code' => 1, 'message' => '保存成功']); } else { echo json_encode(['code' => 0, 'message' => '数据库保存失败']); } } catch (Exception $e) { echo json_encode(['code' => 0, 'message' => '保存异常: ' . $e->getMessage()]); } exit; } // 如果是GET请求,显示页面(可选) echo '请使用POST方法提交数据'; }