作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试在表单中包含图像,图像已正确添加,但在数据库表中添加了两行。
添加数据的表结构如下:
我的 Controller
public function addRecordToTable(){
$this->load->library('form_validation');
$this->form_validation->set_rules('client_id' , 'client_id', 'required');
$this->form_validation->set_rules('l_address' , 'location address', 'required|min_length[3]|max_length[50]');
if ($this->form_validation->run() == false) {
$this->load->model('Clientaccount_model');
$data['bids']=$this->Clientaccount_model->bids();
$data['loads']=$this->Truckeraccount_model->loads();
$this->load->view('footer');
} else {
$array = array(
'client_id' => $this->input->post('client_id'),
'l_address' => $this->input->post('l_address'),
);
$record_id = $this->consignmentupload_model->addData('consignment', $array);
$this->uploadFiles($record_id);
}
}
public function uploadFiles($record_id){
$config = array(
'upload_path' => FCPATH . "/uploads/",
'allowed_types' => 'jpg|png|jpeg',
'overwrite' => TRUE,
);
$this->load->library('upload', $config);
$files = $_FILES['uploads'];
foreach ($files['name'] as $key => $filename) {
$_FILES['uploads[]']['name'] = $files['name'][$key];
$_FILES['uploads[]']['type'] = $files['type'][$key];
$_FILES['uploads[]']['tmp_name'] = $files['tmp_name'][$key];
$_FILES['uploads[]']['error'] = $files['error'][$key];
$_FILES['uploads[]']['size'] = $files['size'][$key];
$config['file_name'] = $filename;
$this->upload->initialize($config);
if (isset($_FILES['uploads[]']['name']) && !empty($_FILES['uploads[]']['name'])) {
if ( ! $this->upload->do_upload('uploads[]')) {
$error = array('error' => $this->upload->display_errors());
} else {
$uploads[] = $this->upload->data();
$array = array(
'record_id' => $record_id,
'filename' => $_FILES['uploads[]']['name'],
'size' => $_FILES['uploads[]']['size']
);
$this->consignmentupload_model->addData('consignment', $array);
}
}
}
redirect(site_url('clientaccount_ctrl'));
}
我的模型
public function addData($table, $array)
{
$this->db->insert($table, $array);
return $this->db->insert_id();
}
最佳答案
after your first insert that is,
$record_id = $this->consignmentupload_model->addData('consignment', $array);
you have to update the same row with your image,
so in uploadFiles instead of, $this->consignmentupload_model->addData('consignment', $array);
make a new model function updateData() where you update the same row which was inserted earlier, pass the luggage_id to updateData()
关于php - 使用 codeigniter 以表单上传图像时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39111874/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!