php - (CodeIgniter 查询生成器)即使插入过程在后台运行良好,我也收到数据库插入错误

标签 php mysql codeigniter

public function answerSurvey() {
    $id = $this->uri->segment(3);
    $i = 0;        
    $reserved = $this->mAttendee->getReserve($id, $this->session->username);
    $zxc = $reserved->registration_id;
    foreach ($_POST as $sample) {
        $value = $this->input->post("question_$i");
        $question = $this->input->post("id_$i");
        $qwe = array('answer_value' => $value, 'question_id' => $question, 'user_id' => $id);
        $this->MSurvey->addAnswer($qwe,$zxc);
        $i++;
    }
    redirect(base_url() . "attendee/viewEvent/$id");

上面是我的 Controller 中的代码片段,我将循环后值中的数据传递到数据库中。当我运行此方法时,我收到一个数据库错误,内容类似于

'answer_value' and 'question_i' cannot be null

尽管如您所见,我通过 $value 和 $question 变量将数据传递到这些值。当我检查数据库时,插入过程似乎工作正常,因为我的数据已传递到表中。为什么我会收到此错误以及如何修复它?我当前的解决方案是禁用数据库配置中的调试。谢谢。

最佳答案

我发现数组 $_POST 中的元素比您在循环中预期的要多得多,例如。对于数组

array(
    'question_0' => 'a',
    'question_1' => 'b',
    'id_0' => '1',
    'id_1' => '2',
);

您的循环将迭代 4 次,因此对于第三次和第四次迭代,它将生成错误,因为 $_POST 中不存在 question_3question_4 等项目 所以 $this->input->post("id_$i") 返回 NULL

关于php - (CodeIgniter 查询生成器)即使插入过程在后台运行良好,我也收到数据库插入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43106467/

相关文章:

php - 编译错误 : Cannot use positional argument after named argument

php - 为什么无法访问我的网站(在 Wordpress 中)?

mysql - 如何在mysql中使用范围值搜索

php - 在 CodeIgniter Activerecord 中执行 WHERE !=

php - PHP 上的 AES256 字符串加密和 iPhone 上的解密

php - UTF-8贯穿始终

c# - 如何在 C# 中创建与 Mysql 的全新连接?

MySQL 查询以计算值

php - Titanium Studio/PHP - 在 iOS 上的 webview 中打开链接

php - 在 Codeigniter 中获取 JSON 响应