codeigniter文件上传,两次提交到数据库



所以我遇到了一个非常奇怪的问题。我有一个表格,收集一些用户的详细信息和他们的个人资料图片。一切都正常运转。

我希望能够有一个默认的图像,如果用户不上传图片。使用下面的代码,如果他们不上传图片,一切都很好,所有的细节和默认图像都会保存到数据库中。

但奇怪的是,如果你上传了一张照片,它会两次将详细信息插入数据库。一次使用上传的图片,然后再次使用默认图片。我一辈子都弄不明白,我花了好几个小时才弄明白。我很感激你的帮助。

控制器:

function addNewStaff()
{
if ($this->isAdmin() == TRUE) {
$this->loadThis();
} else {
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required|max_length[128]');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|max_length[128]');
$this->form_validation->set_rules('roleId', 'Role Id', 'trim|required|numeric');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|max_length[128]');
$this->form_validation->set_rules('password', 'Password', 'required|max_length[20]');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required|matches[password]|max_length[20]');
$this->form_validation->set_rules('mobile', 'Mobile Number', 'required|min_length[10]');
$this->form_validation->set_rules('start_date', 'Start Date', 'required');
$this->form_validation->set_rules('contract_type', 'Contract Type', 'required');
if ($this->form_validation->run() == FALSE) {
$this->addNewStaff_View();
$this->session->set_flashdata('error', 'Something Went Wrong');
} else {                                         
$first_name = ucwords(strtolower($this->security->xss_clean($this->input->post('first_name'))));
$last_name = ucwords(strtolower($this->security->xss_clean($this->input->post('last_name'))));
$roleId = $this->input->post('roleId');
$email = strtolower($this->security->xss_clean($this->input->post('email')));
$password = $this->input->post('password');
$mobile = $this->security->xss_clean($this->input->post('mobile'));
$start_date = $this->input->post('start_date');
$contract_type = $this->input->post('contract_type');
$date = DateTime::createFromFormat('d/m/Y', $start_date);
$formatteddate = $date->format('Y-m-d'); 


if(!empty($_FILES['file']['name'])){
// Set preference
$config['upload_path'] = 'content/uploads/'; 
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = '1024'; // max_size in kb
$config['file_name'] = $first_name . $last_name . $date = date('Y-m-d');

//Load upload library
$this->load->library('upload',$config); 

// File upload
if($this->upload->do_upload('file')){
// Get data about the file
$uploadData = $this->upload->data();
$profile_picture = $uploadData['file_name'];

}                   
}                 
else{
$profile_picture = 'default.png';
}     

$staffInfo = array(
'email' => $email, 'password' => getHashedPassword($password), 'roleId' => $roleId, 'first_name' => $first_name, 'last_name' => $last_name,
'mobile' => $mobile, 'start_date' => $formatteddate, 'contract_type' => $contract_type, 'profile_pic'=> $profile_picture
);
$result = $this->staff_model->addNewStaff($staffInfo);
if ($result > 0) {
$this->session->set_flashdata('success', 'Staff member added successfully');
} else {
$this->session->set_flashdata('error', 'staff member creation failed');
}
redirect('staffList');
}
}
}

型号:

function addNewStaff($staffInfo)
{
$this->db->trans_start();
$this->db->insert('tbl_staff', $staffInfo); 
$insert_id = $this->db->insert_id();
$this->db->trans_complete();
return TRUE;       

}

控制器代码:-

function addNewStaff()
{
if ($this->isAdmin() == TRUE) {
$this->loadThis();
} else {
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required|max_length[128]');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|max_length[128]');
$this->form_validation->set_rules('roleId', 'Role Id', 'trim|required|numeric');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|max_length[128]');
$this->form_validation->set_rules('password', 'Password', 'required|max_length[20]');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required|matches[password]|max_length[20]');
$this->form_validation->set_rules('mobile', 'Mobile Number', 'required|min_length[10]');
$this->form_validation->set_rules('start_date', 'Start Date', 'required');
$this->form_validation->set_rules('contract_type', 'Contract Type', 'required');
if ($this->form_validation->run() == FALSE) {
$this->addNewStaff_View();
$this->session->set_flashdata('error', 'Something Went Wrong');
} else {                                         
$first_name = ucwords(strtolower($this->security->xss_clean($this->input->post('first_name'))));
$last_name = ucwords(strtolower($this->security->xss_clean($this->input->post('last_name'))));
$roleId = $this->input->post('roleId');
$email = strtolower($this->security->xss_clean($this->input->post('email')));
$password = $this->input->post('password');
$mobile = $this->security->xss_clean($this->input->post('mobile'));
$start_date = $this->input->post('start_date');
$contract_type = $this->input->post('contract_type');
$date = DateTime::createFromFormat('d/m/Y', $start_date);
$formatteddate = $date->format('Y-m-d'); 


if(!empty($_FILES['file']['name'])){
// Set preference
$config['upload_path'] = 'content/uploads/'; 
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = '1024'; // max_size in kb
$config['file_name'] = $first_name . $last_name . $date = date('Y-m-d');

//Load upload library
$this->load->library('upload',$config); 

// File upload
if($this->upload->do_upload('file')){
// Get data about the file
$uploadData = $this->upload->data();
$profile_picture = $uploadData['file_name'];

}                   

else{
$profile_picture = 'default.png';
}     
}          
$staffInfo = array(
'email' => $email, 'password' => getHashedPassword($password), 'roleId' => $roleId, 'first_name' => $first_name, 'last_name' => $last_name,
'mobile' => $mobile, 'start_date' => $formatteddate, 'contract_type' => $contract_type, 'profile_pic'=> $profile_picture
);
$result = $this->staff_model->addNewStaff($staffInfo);
if ($result > 0) {
$this->session->set_flashdata('success', 'Staff member added successfully');
} else {
$this->session->set_flashdata('error', 'staff member creation failed');
}
redirect('staffList');
}
}
}

相关内容

  • 没有找到相关文章

最新更新