Php 在codeigniter中上传mysql表中的图像和数据

Php 在codeigniter中上传mysql表中的图像和数据,php,codeigniter,Php,Codeigniter,我正试图上传一个带有汽车品牌、年份、租赁和付款数据的图像 随着图像上传。我的图像路径也被插入到数据库中,但我仍然不知道如何将两者结合起来。 我得到了一个错误数组到字符串的转换,它没有输入我的数据和上传图像 cars表: car_make | car_model | car_year | car_lease | car_payment | imgpath acura | G37 Sedan | 2009 | 48 | 350 | assets/imag

我正试图上传一个带有汽车品牌、年份、租赁和付款数据的图像 随着图像上传。我的图像路径也被插入到数据库中,但我仍然不知道如何将两者结合起来。 我得到了一个
错误数组到字符串的转换
,它没有输入我的数据和上传图像

cars表:

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>&nbsp;&nbsp;&nbsp;</td>


</tr>
<tr>
  <td>Car Model:</td>
  <td><input type="text" name="car_model" class="textbox" tabindex="2"/></td>
  <td>&nbsp;</td>

</tr>
<tr>
  <td>Car Year:</td>
  <td><input type="text" name="car_year" class="textbox" tabindex="3"/></td>
  <td>&nbsp;</td>

</tr>
<tr>
  <td>Car Lease:</td>
  <td><input type="text" name="car_lease" class="textbox" tabindex="4"/></td>
  <td>&nbsp;</td>

</tr>
<tr>
  <td>Car Payment:</td>
  <td><input type="text" name="car_payment" class="textbox" tabindex="5"/></td>
  <td>&nbsp;</td>

</tr>

<tr>
  <td>Car Image:</td>
  <td><input type="file" name="userfile" size="20" /></td>
  <td>&nbsp;</td>

</tr>

<tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
</tr>

</table>


<input type="submit" name="submit" value="submit" id="button" class="button" style="margin-left:140px;"  />

<?php

echo form_close();
?>
public function car\u validation\u do\u upload(){
$this->load->library('form_validation');
$this->form_validation->set_规则('car_make','car make','required');
$this->form_validation->set_规则('car_model'、'car model'、'required');
$this->form_validation->set_规则('car_year'、'car year'、'required');
$this->form_validation->set_规则('car_lease'、'car lease'、'required');
$this->form_validation->set_规则('car_payment'、'car payment'、'required');
如果($this->form\u validation->run()==TRUE){
$config['upload_path']='./assets/images/';
$config['allowed_types']='gif | jpeg | jpg | png';
$this->load->library('upload',$config);
$newRow=array(“car\u make”=>$this->input->post('car\u make'),
“汽车模型”=>this->input->post('car\u model'),
“汽车年”=>this->input->post('car\u year'),
“汽车租赁”=>this->input->post('car\u lease'),
“汽车付款”=>this->input->post('car\u payment');
$data=array('upload'=>$this->upload->data());
$result=array\u merge($newRow,$data);
$this->load->model(“model_用户”);
$this->model_users->insert1($result);
$this->session->set_flashdata('message','car inserted into database!');
重定向('main/insertCar');
}
否则{
$this->load->view('insert_car');
}
}
型号:
公共函数insert1($data){
$this->db->insert(“汽车”,$data);
}
看法

插入汽车

汽车制造: 车型: 车年: 汽车租赁: 汽车付款: 汽车图像:
更改此选项:

$data = array('upload' => $this->upload->data());

$result = array_merge($newRow, $data);
为此:

  if ($this->upload->do_upload()){
    $image_data = $this->upload->data();
    //this will create a new key with the name upload in your newRow
    //so theres no need to use the function array_merge
    $newRow['imgpath'] ='assets/images/'.$image_data['file_name'];
}
else{
    //do something here
    echo 'error uploading image'
}
$image\u数据变量将包含有关上载图像的此信息

 Array
(
    [file_name]    => mypic.jpg
    [file_type]    => image/jpeg
    [file_path]    => /path/to/your/upload/
    [full_path]    => /path/to/your/upload/jpg.jpg
    [raw_name]     => mypic
    [orig_name]    => mypic.jpg
    [client_name]  => mypic.jpg
    [file_ext]     => .jpg
    [file_size]    => 22.2
    [is_image]     => 1
    [image_width]  => 800
    [image_height] => 600
    [image_type]   => jpeg
    [image_size_str] => width="800" height="200"
)
您可以在codeingiter的用户指南中找到有关文件上载类的所有信息:

我在向表格插入值时出错。嗨,我试过你的代码,它说的是未定义的变量:数组。我已经阅读了用户指南,我的表单更复杂,因为我试图将文本和图像上传到同一个表中。我还能试什么?谢谢。这行:$newRow['upload']=$array['full_path'];我是否应该将“完整路径”更改为其他路径?对不起,它应该是$image\u数据,而不是$array。我想我离它越来越近了。“字段列表”中的未知列“用户文件”插入
汽车
汽车品牌
汽车型号
汽车年份
汽车租赁
汽车付款
imgpath
用户文件
)值('bmw',525i',2004',24',350',1',C:/wamp/www/autoline/assets/images/bmw5.jpg'))在回答中,它已被编辑,但这里您再次看到$newRow['imgpath']='assets/images/'。$image\u data['file\u name'];