我正在尝试上传带有汽车制造商、年份、租赁和付款数据的图像以及图像上传。我的图像路径也被插入到数据库中,但我仍然不知道如何将两者结合起来。我得到一个error Array to string conversion
并且它没有输入我的数据和图像上传。
汽车表:
car_make | car_model | car_year | car_lease | car_payment | imgpath
acura | G37 Sedan | 2009 | 48 | 350 | assets/images/acura_1.jpg
控制器:
public function car_validation_do_upload() {
$this->load->library('form_validation');
$this->form_validation->set_rules('car_make', 'Car Make', 'required');
$this->form_validation->set_rules('car_model', 'Car Model', 'required');
$this->form_validation->set_rules('car_year', 'Car Year', 'required');
$this->form_validation->set_rules('car_lease', 'Car Lease', 'required');
$this->form_validation->set_rules('car_payment', 'Car Payment', 'required');
if($this->form_validation->run() == TRUE) {
$config['upload_path'] = './assets/images/';
$config['allowed_types'] = 'gif|jpeg|jpg|png';
$this->load->library('upload', $config);
$newRow = array("car_make" => $this->input->post('car_make'),
"car_model" => $this->input->post('car_model'),
"car_year" => $this->input->post('car_year'),
"car_lease" => $this->input->post('car_lease'),
"car_payment" => $this->input->post('car_payment'));
$data = array('upload' => $this->upload->data());
$result = array_merge($newRow, $data);
$this->load->model("model_users");
$this->model_users->insert1($result);
$this->session->set_flashdata('message', 'car inserted into database!');
redirect('main/insertCar');
}
else {
$this->load->view('insert_car');
}
}
Model:
public function insert1($data) {
$this->db->insert("cars", $data);
}
View
<?php
//echo form_open('main/car_validation_insert');
echo form_open_multipart('main/car_validation_do_upload');
echo validation_errors();
if($this->session->flashdata('message')){
echo $this->session->flashdata('message');
}
?>
<table width="504" style="margin-left:15px;">
<tr>
<td colspan="2"><p class="table-title">Insert Car</p></td>
<td></td>
</tr>
<tr>
<td>Car Make: </td>
<td><input type="text" name="car_make" class="textbox" tabindex="1"/></td>
<td> </td>
</tr>
<tr>
<td>Car Model:</td>
<td><input type="text" name="car_model" class="textbox" tabindex="2"/></td>
<td> </td>
</tr>
<tr>
<td>Car Year:</td>
<td><input type="text" name="car_year" class="textbox" tabindex="3"/></td>
<td> </td>
</tr>
<tr>
<td>Car Lease:</td>
<td><input type="text" name="car_lease" class="textbox" tabindex="4"/></td>
<td> </td>
</tr>
<tr>
<td>Car Payment:</td>
<td><input type="text" name="car_payment" class="textbox" tabindex="5"/></td>
<td> </td>
</tr>
<tr>
<td>Car Image:</td>
<td><input type="file" name="userfile" size="20" /></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
<input type="submit" name="submit" value="submit" id="button" class="button" style="margin-left:140px;" />
<?php
echo form_close();
?>