在Codeigniter中将表单属性和图像路径插入DB

时间:2013-12-28 13:46:28

标签: php codeigniter

我试图让用户使用以下表单上传图片和整数:

<?php 
  echo form_open_multipart('upload/do_upload'); 
  $image_data = array(
    'name'=>'userfile'
  );
  echo "<br>File:  ";
  echo form_upload($image_data);
  echo "<br />";
  $int_data=array(
    'name' =>'int',
    'id' =>'int'
  );
  echo "<br><br>Integer:  ";
  echo form_input($int_data);
?>
<?php echo form_submit('','Upload'); ?>
<?php echo form_close(); ?>

之后,我正在使用这个do_upload函数来处理图像并将其上传到服务器:

function do_upload(){
  $config['upload_path']='./uploads';
  $config['allowed_types']='gif|jpeg|png|jpg|bmp';
  $config['max_size']='5000';
  $config['max_width']='2000';
  $config['max_height']='2000';
  $this->load->library('upload',$config);

  if( !$this->upload->do_upload()){
    $error = array('error'=>$this->upload->display_errors());
    $this->load->view('header');
    $this->load->view('upload_form',$error);
    $this->load->view('footer');
  }
  else{
    //RESIZE THE IMAGE 
    $data=array('upload_data'=>$this->upload->data());
    $this->resize($data['upload_data']['full_path'],$data['upload_data']['file_name']);
    $this->insert_post();
    $this->load->view('header');
    $this->load->view('upload_success',$data);
    $this->load->view('footer');
  }
}

注意控制器中的insert_post函数?这是:

function insert_post(){
  if($_POST){
    $data=array(
      'int'=> $_POST['int'],
      'image_path'=>$upload_data['full_path'] ,
      'active'=>1
    );
    $this->post->insert_post($data);
    redirect(base_url().'posts/');
  } else {
      $this->load->view('upload_success');
  }
}

模型中的insert_post函数如下所示:

function insert_post($data){
  $this->db->insert('posts',$data);
  return $this->db->insert_id();
}

现在问题 - 我很难将数据插入表中。你能指点一下问题的方向吗?

1 个答案:

答案 0 :(得分:1)

您必须将数据数组传递给插入函数

$this->insert_post($insertData);

其中$insertData将保存db字段名称及其对应的db表值。 在你的情况下

$this->insert_post($data['upload_data']);

并从控制器中的函数访问

function insert_post($upload_data){
...........
}

查看更多信息here