【问题标题】:NULL Value inserted in Database (Codeigniter)NULL 值插入数据库(Codeigniter)
【发布时间】:2016-04-15 05:44:09
【问题描述】:

我正在尝试在数据库中插入数据,但是以某种方式 NULL 被插入到了数据库中

这是我的控制器

foreach($this->input->post('resume_id') as $key =>$value ){

                $ResumeInsert[$key]['resume_keyid'] = $Resume['resume_id'][$key];
                $ResumeInsert[$key]['employer_name'] = $Resume['employer_name'][$key];
                $ResumeInsert[$key]['start_Date'] = $Resume['start_Date'][$key];
                $ResumeInsert[$key]['end_date'] = $Resume['end_date'][$key];
                $ResumeInsert[$key]['type_id'] = $Resume['type_id'][$key];
                $ResumeInsert[$key]['position'] = $Resume['position'][$key];
                $ResumeInsert[$key]['responsibility'] = $Resume['responsibility'][$key];
                $ResumeInsert[$key]['Skills'] = $Resume['Skills'][$key];

                if(isset($Resume['id'][$key]) ){
                    $Key_Resume__ExistIDs[]=$Resume['id'][$key];
                    $ResumeUpdate[$key]=$ResumeInsert[$key];
                    $ResumeUpdate[$key]['resume_id']=$Resume['id'][$key];
                    unset($ResumeInsert[$key]);
                }
                 else{

                     $ResumeInsert[$key]['resume_id'] = $GetLastID;
                     print_r ($ResumeInsert[$key]);exit;
                     $GetLastID++;
                } 
            }
            $idsToDelete='';
            if(empty($ResumeInsert) &&  empty($ResumeUpdate)){

                $idsToDelete=array_diff($Key_Resume_IDs,$Key_Resume__ExistIDs);
            }
            $status=$this->Resume_model->ProcessData($idsToDelete,$ResumeUpdate,$user_id,$ResumeInsert,$imgInsert,$imgUpdate);

            redirect('Resume','refresh');   

这是我的模型代码

function ProcessData($idsToDelete,$tbl_resumeUpdate,$user_id,$tbl_resumeInsert,$imgInsert,$imgUpdate){
    $this->db->trans_start();
    if(!empty($idsToDelete)){
        $this->delete_tbl_resume($idsToDelete);
    }
    if(!empty($tbl_resumeUpdate)){
        //echo "up";exit;
        $this->update_tbl_resume($tbl_resumeUpdate);
    }
    if(!empty($tbl_resumeInsert)){
        //echo "int";exit;
        $this->insert_tbl_resume($user_id,$tbl_resumeInsert);
    }
    if(!empty($imgInsert)){
        $this->insert_tbl_file_paths($imgInsert);
    }
    if(!empty($imgUpdate)){
        $this->update_tbl_file_paths($imgUpdate);
    }
    return $this->db->trans_complete();
}

这是插入查询

function insert_tbl_resume($id,$arrtbl_resume){
    $this->db->insert_batch('tbl_resume', $arrtbl_resume); 

}

在上面的代码中,空值插入到数据库中。 当我打印上面的查询时,它显示为空白

有什么帮助吗?

【问题讨论】:

  • 在函数insert_tbl_resume 中检查$arrtbl_resume 的值了吗?
  • 是的..它显示,**Array([0] => Array([resume_keyid] => [employer_name] => [start_Date] => [end_date] => [type_id] => [职位] => [职责] => [技能] => [resume_id] => 52 ) ) **
  • 在 for 循环 foreach($this->input->post('resume_id') as $key =>$value ){ 中检查 $Resume 数组的值。还要确保在调用插入查询函数之前正在执行for 循环。
  • @SankarV :- 空白
  • 那么当你向插入函数发送空白数组时,你希望在数据库中插入什么?

标签: php mysql codeigniter


【解决方案1】:

您应该使用form_validation 库。我给你一个例子,你可以编辑和使用它。

在 autoload.php 中,将$autoload['libraries'] = array(); 行编辑为:

$autoload['libraries'] = array('form_validation');

然后,在您的控制器文件中使用form_validation。例如:

$this->form_validation->set_rules('resume_keyid', 'Resume ID', 'required');

if ($this->form_validation->run() == FALSE) 
{
   $this->index() // if there is an error, user will redirect to this function
}

else 
{
  $this->Resume_model->ProcessData();
}

另外请在您的模型中使用$this->input->post('resume_keyid', TRUE); 结构。 “TRUE”表示“打开 XSS 过滤器”。因为在 CI 3 中,它默认关闭。如果你不想要它,只需删除它。如果使用 CI 2,则无需添加“TRUE”。

一些建议:

1 - 命名函数时不要使用骆驼法。例如;使用process_data() 而不是processData()

2 - 检查CI Form Validation Document 了解所有详细信息(例如所有参考资料)

3 - 我认为你可以使用$this->db->insert();,只需创建一个数组并POST 它。如果你做到了,你就会明白哪里出了问题。

【讨论】:

    猜你喜欢
    • 2014-09-24
    • 1970-01-01
    • 2013-03-28
    • 2015-10-07
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2015-07-02
    • 2014-05-04
    相关资源
    最近更新 更多