【问题标题】:Insert form property and image path to DB in Codeigniter在 Codeigniter 中插入表单属性和图像路径到 DB
【发布时间】:2013-12-28 22:56:57
【问题描述】:

我正在尝试让用户使用此表单上传图像和整数:

<?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();
}

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

【问题讨论】:

  • @LLL :更改原始帖子时请多加小心。你复制粘贴错了:-(
  • 你的模型加载了吗?
  • 哦,伙计!是的,就是这样。

标签: php codeigniter


【解决方案1】:

你必须将一个数据数组传递给你的插入函数

$this->insert_post($insertData);

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

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

并从控制器中的函数访问

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

查看更多信息here

【讨论】:

  • 这是一种双向的东西——我根本没有加载模型,也没有将数组传递给函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多