gpt4 book ai didi

php - 使用 codeigniter 以表单上传图像时遇到问题

转载 作者:行者123 更新时间:2023-11-29 20:30:19 26 4
gpt4 key购买 nike

我尝试在表单中包含图像,图像已正确添加,但在数据库表中添加了两行。

添加数据的表结构如下:

我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com