繁体   English   中英

(CodeIgniter查询生成器)即使在后台插入过程正常,我仍收到数据库插入错误

[英](CodeIgniter query builder) I'm getting a database insert error even though the insertion process works fine in the background

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");

上面是控制器中的代码片段,我在其中将循环的post值中的数据传递到数据库中。 当我运行此方法时,出现数据库错误,提示类似

“ answer_value”和“ question_i”不能为空

即使您可以看到,我还是通过$ 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM