2015-11-05 61 views
1

我有一个窗体,现在它已经被窃听了一段时间,当我将所有信息填入窗体中时,它似乎非常快速地插入它,但是如果一个或多个字段为空,根本不起作用 我正在使用的查询插入到数据库中。我想弄清楚,如果我可以使用Insert_delay,也许它会解决问题?任何可能的解决方案或改变我需要做关于Php codeigniter插入查询的建议

function student($param1 = '', $param2 = '', $param3 = '') 
{ 
    if ($this->session->userdata('admin_login') != 1) 
     redirect('login', 'refresh'); 
    if ($param1 == 'create') { 
     $data['name']  = $this->input->post('name');   
     $data['sex']  = $this->input->post('sex'); 
     $data['address'] = $this->input->post('address'); 
     $data['phone']  = $this->input->post('phone'); 
     $data['email']  = $this->input->post('email'); 
     $data['password'] = $this->input->post('password'); 
     $data['year']  = $this->input->post('year'); 
     $data['rate'] = $this->input->post('rate'); 
     $data['class_id'] = $this->input->post('class_id'); 
     $data['class_id2'] = $this->input->post('class_id2'); 
     $data['class_id3'] = $this->input->post('class_id3'); 
     $data['class_id4'] = $this->input->post('class_id4'); 
     $data['parent_id'] = $this->input->post('parent_id'); 
     $data['roll']  = $this->input->post('roll'); 
     $this->db->insert_('student', $data); 
     $student_id = $this->db->insert_id(); 

     $this->session->set_flashdata('flash_message' , get_phrase('Student_added_successfully')); 
     $this->email_model->account_opening_email('student', $data['email']); //SEND EMAIL ACCOUNT OPENING EMAIL 
     redirect(base_url() . 'index.php?admin/student_add/' . $data['class_id'], 'refresh'); 
+0

然而,即使在出现故障的情况下,有时数据被添加到表 – Anasbzr

+0

它也依赖于db表字段的默认值。如果预料到的事情必须通过。否则,如果数据不是强制的,则字段的默认值可以设置为“NULL”。 – Tpojka

+0

默认情况下,我已将它们全部设置为空。并且只剩下PK和FK,而不是。但仍然是相同的问题 – Anasbzr

回答

0

在控制器

function student($param1, $param2 $param3) 
{ 
    if ($this->session->userdata('admin_login') != 1) 
    { 
     redirect('login', 'refresh'); 
    } 
    else 
    { 
     if($param1 == 'create') { 

      $result = $this->model_name->insert_data(); 

      if ($result != FALSE) { 

       $email => $this->input->post('email');     
       $confirm = $this->email_model->account_opening_email('student', $email); # do same as insert 

       if ($confirm != FALSE) { 

        $this->session->set_flashdata('flash_message' , get_phrase('Student_added_successfully')); 
        redirect(base_url() . 'index.php?admin/student_add/' . $data['class_id'], 'refresh'); 
       } 
       else 
       { 
        echo "Mail send failed"; 
       } 

      } 
      else 
      { 
       echo "param1 is not create"; 
      }  
     } 
    } 
} 

在模型

public function insert_data() 
{ 
    # to isert data, I add them in to one array 
    $data = array(
     'name' => $this->input->post('name'), 
     'sex' => $this->input->post('sex'), 
     'address' => $this->input->post('address'), 
     'phone' => $this->input->post('phone'), 
     'email' => $this->input->post('email'), 
     'password' => $this->input->post('password'), 
     'year' => $this->input->post('year'), 
     'rate' => $this->input->post('rate'), 
     'class_id' => $this->input->post('class_id'), 
     'class_id2' => $this->input->post('class_id2'), 
     'class_id3' => $this->input->post('class_id3'), 
     'class_id4' => $this->input->post('class_id4'), 
     'parent_id' => $this->input->post('parent_id'), 
     'roll' => $this->input->post('roll') 
    ); 

    if (!$this->db->insert('student', $data)) { 

     # insert FAILED 
     return FALSE; 
    } 
    else{ 

     # insert SUCCESS   
     $slast_id = $this->db->insert_id(); # get last insert ID 
     return = $slast_id; 
    } 

} 
+0

感谢的人,这是一个很大的变化 – Anasbzr