You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter中Ajax+CURL提交数据始终返回NULL值的问题

哥们儿,我帮你捋捋这个问题——你现在用CodeIgniter+Ajax提交数据,结果插入数据库时noreg、user_id这些字段全是NULL,直接触发了SQL的非空约束报错,对吧?咱一步步排查:

第一步:先确认前端Ajax有没有把数据发出去

  • 打开浏览器开发者工具(F12),切换到Network标签,触发Ajax请求后,查看对应的POST请求:
    • 如果是普通表单格式,看Form Data里有没有noreg、user_id这些参数,值是不是正确的;
    • 如果是JSON格式,看Request Payload里的JSON数据是否完整。
  • 检查你的Ajax代码,别犯这些低级错误:
    // 错误示范:没传数据或者参数名写错
    $.ajax({
      url: '/your-controller/verify-action',
      type: 'POST',
      data: {}, // 这里是空的,肯定收不到!
      success: function(res) {}
    });
    
    // 正确写法:确保参数名和后端要的一致
    $.ajax({
      url: '<?= base_url('your-controller/verify-action') ?>',
      type: 'POST',
      dataType: 'json',
      // 如果是JSON格式,必须加这个请求头
      contentType: 'application/json',
      data: JSON.stringify({
        noreg: $('#noreg-input').val(),
        user_id: $('#user-id').val(),
        verified: 1,
        level_verif: 1,
        catatan: $('#catatan-textarea').val()
      }),
      error: function(xhr) {
        console.log(xhr.responseText); // 方便看报错信息
      }
    });
    

第二步:检查CodeIgniter后端有没有正确接收数据

CodeIgniter接收POST数据分两种情况,别搞混:

  • 如果是普通表单提交(contentType默认是application/x-www-form-urlencoded),直接用框架的输入类:
    $noreg = $this->input->post('noreg');
    $user_id = $this->input->post('user_id');
    // 先打印看看有没有值,调试完再删掉
    var_dump($this->input->post());
    exit;
    
  • 如果是JSON格式的请求,框架的$this->input->post()可能拿不到,得直接读原始输入:
    $raw_input = file_get_contents('php://input');
    $post_data = json_decode($raw_input, true);
    $noreg = $post_data['noreg'] ?? null;
    $user_id = $post_data['user_id'] ?? null;
    // 同样打印调试
    var_dump($post_data);
    exit;
    

⚠️ 重点:参数名必须和前端完全一致!比如前端传的是noreg,后端不能写成no_reg,大小写也要对应。

第三步:检查数据库插入的代码有没有正确赋值

看你报错里的SQL语句VALUES (NULL, NULL, NULL, 0, ''),说明你插入时根本没把接收的参数传进去!

  • 错误示范:变量没赋值就直接插
    $insert_data = [
      'noreg' => null, // 这里应该用接收的$noreg啊!
      'user_id' => null,
      'verified' => null,
      'level_verif' => 0,
      'catatan' => ''
    ];
    $this->db->insert('VKLAIM_VERIF', $insert_data);
    
  • 正确写法:把接收的参数赋值进去,还要做非空判断
    // 先判断必填字段是否存在,提前拦截错误
    if(empty($noreg) || empty($user_id)) {
      echo json_encode(['status' => 'error', 'msg' => 'noreg和user_id不能为空']);
      return;
    }
    
    $insert_data = [
      'noreg' => $noreg,
      'user_id' => $user_id,
      'verified' => $this->input->post('verified') ?? 1,
      'level_verif' => 1,
      'catatan' => $this->input->post('catatan') ?? ''
    ];
    $this->db->insert('VKLAIM_VERIF', $insert_data);
    

额外提醒:如果是后端用CURL调用其他接口获取数据

要是你的逻辑是:前端Ajax给后端,后端用CURL调用另一个接口拿数据,再插入数据库——那还要检查CURL的返回值:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://your-api-url/get-data');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
  'some_param' => 'value'
]));
$response = curl_exec($ch);
curl_close($ch);

$api_data = json_decode($response, true);
// 检查api_data里有没有noreg等字段,别是空的
var_dump($api_data);
exit;

按这个流程一步步排查,先确认数据从前端到后端有没有传对,再确认后端有没有把数据传到数据库里,肯定能找到问题所在!

内容的提问来源于stack exchange,提问作者Agung

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:28:19