【问题标题】:how to pass the data in the post so that we can call the id?如何在帖子中传递数据以便我们可以调用id?
【发布时间】:2019-06-17 03:56:26
【问题描述】:

如何传递post中的数据以便我们调用id?

因为我们正在尝试获取问题的“qtopic_id”,但它不起作用,并且它一直给我一个空值。

我已尝试声明 qtopic_id= 19 以查看其是否保存在 qtopic_id 列中。 我不必将特定的 id 值保存在以下列中,这样它就不会仅保存在该特定 id 上,而是会保存在其相应的 qtopic_id 上。

控制器

public function addChoices(){
$this->form_validation->set_rules('ques','Question','required');
$this->form_validation->set_rules('ch_des1','Choices','required');
$this->form_validation->set_rules('ch_des2','Choices','required');
$this->form_validation->set_rules('ch_des3','Choices','required');
$this->form_validation->set_rules('ch_des4','Choices','required');
$this->form_validation->set_rules('ans','Answer','required');
$this->form_validation->set_error_delimiters('<div class="text-danger">','</div>');




if($this->form_validation->run() ){
echo $qtopic_id ;
$data['ques']    = ($this->input->post('ques'));
$data['ch_des1'] = ($this->input->post('ch_des1'));
$data['ch_des2'] = ($this->input->post('ch_des2'));
$data['ch_des3'] = ($this->input->post('ch_des3'));
$data['ch_des4'] = ($this->input->post('ch_des4'));
$data['ans']     = ($this->input->post('ans'));

if($id=$this->queries->registerChoices($data) )
{

$this->session->set_flashdata('message','Test Added Succesfully');
return redirect('admin/testtwo');
}
else {
$this->session->set_flashdata('message','Failed to Add Test');  
}
return redirect('admin/testtwo');
}   

else {
$this->addQuestion();
}   

}


}

型号:

----更新---

  public function registerChoices($data) {
              echo $this->input->post('qtopic_id');


            $question_arr = [
                'ques' => $data['ques'],
                'qtopic_id' => 19


            ];

            $choices_arr = [
                'ques'    => $data['ques'],
                'ch_des1' => $data['ch_des1'],
                'ch_des2' => $data['ch_des2'],
                'ch_des3' => $data['ch_des3'],
                'ch_des4' => $data['ch_des4'],
                'ans'     => $data['ans']

            ];

   //          echo "<pre>";
            // var_dump($question_arr);
            // var_dump($choices_arr);
            // exit;

            $this->db->insert('tbl_choices',$choices_arr);
            $this->db->insert('tbl_question',$question_arr);
                    return $this->db->insert_id();


        }

error messages that i encountered

【问题讨论】:

  • model 或 addchoices 中显示的 null 错误在哪里,您尝试从哪里发布 qtopic_id 。在你注册选择的模型中,你试图打印 qtopic_id 但你没有从控制器发送参数 qtopic_id,并且你再次尝试在模型中设置 qtopic_id 值,它有点混淆你想要什么
  • 您的问题还不清楚,请在此处添加更多代码和更多说明
  • 我已经包含了错误并添加了一些额外的代码..谢谢你!!

标签: php codeigniter


【解决方案1】:

您的寄存器选择代码有点不清楚。 insert_id 不是接受数据的函数,也不是插入函数,它只是在执行插入查询后返回最后插入的 id。我认为你想要的是这样的:

function registerChoices($data) {

    if ($this->db->insert('tablename', $data)) {
        return $this->db->insert_id();
    }
    return false;

}

用法:

$last_insert_id = $this->somemodel->registerChoices($data);

if ($last_insert_id) {
    echo "item with id $last_insert_id was created!";
} else {
    show_error('Query failed!');
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 2022-01-08
    相关资源
    最近更新 更多