使用codeigniter将新项添加到我的数据库时出错

时间:2016-12-21 13:31:07

标签: php codeigniter

我在使用codeigniter提交表单时遇到此错误。即使我一直在关注文档,我也需要帮助。 我正在使用2.x版本的codeigniter

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Products::$input‐

Filename: core/Model.php

Line Number: 52


Fatal error: Call to undefined function post() in /opt/lampp/htdocs/gamingplace/application/models/product_model.php on line 63

这是我模型中的代码

 //add new product
        public function addProduct(){
          $data = array(
            'category_id' => $this->input‐>post('category_id'),
            'title' => $this->input‐>post('title'),
            'description' => $this->input‐>post('description'),
            'price' => $this->input‐>post('price')
          );
          if($_FILES['image']['error'] == 0){
            $data['image'] = $this->upload->file_name;
            return $this->db->insert('products',$data);
            }
          }
        }

这是我控制器中的代码

public function addProduct()
{
  if($_FILES['image']['error'] == 0){
    //setting  image preferences
    $config['upload_path'] = 'uploads/';
    $config['allowed_types'] = 'jpg|png|jpeg|PNG|JPEG|JPG';
    $config['overwrite'] = false;
    $config['quality'] = '100%';
    $config['remove_spaces'] = true;
    $config['max_size']  = '90';// in KB
    $this->load->library('upload',$config);
    echo $this->input->post('image');

    if( ! $this->upload->do_upload('image'))
    {
      $this->session->set_flashdata('message_failed', $this->upload->display_errors('', ''));
      redirect('dashboard');
    }else{
    //Image Resizing
    $config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
    $config['maintain_ratio'] = FALSE;
    $config['width'] = 311;
    $config['height'] = 162;
    $this->load->library('image_lib',$config);
    if ( ! $this->image_lib->resize()){
      $this->session->set_flashdata('message_failed', $this->image_lib->display_errors('', ''));
    }
    //calling a model an its method to add a new product to the database
    if($this->product_model->addProduct()){
      $upload_data = $this->upload->data();
      $this->session->set_flashdata('message', 'A new product,'. $upload_data['file_name'].'has been added!');
    redirect('dashboard');
  }
}
}
}
}

我的观点

<!-- Modal -->
<script src="<?php echo base_url(); ?>assets/js/validation.js" charset="utf-8"></script>
    <div class="modal fade" id="addProduct" role="dialog">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Add Product</h4> <em>if the product category is not in the select option,
               go to add category link to add category before adding this product</em>
          </div>
          <div class="modal-body">
            <?php echo validation_errors('<div class="alert alert-danger">','</div>') ?>
              <form role= "form" action="<?php echo base_url()?>products/addProduct" method="post" name="form" onsubmit="return validate(this)" enctype="multipart/form-data">
              <div class="form-group">
                <input type="text" name="title" class="form-control" placeholder="Enter name of product" required>
              </div>
              <div class="form-group">
                <label>Brief Description of Product</label>
                <textarea name="description" rows="8" cols="80" class="form-control">
                  This is a brand made by Gucci Company. Made of cotton, bla bla bla
                </textarea>
              </div>
              <label>Select Category</label>
              <div class="form-group">
                <select class="form-control" name="category_id">
                  <?php foreach (get_categories_h() as $popular): ?>
                  <option value="<?php echo$popular->id?>"><?php echo $popular->name ?></option>
                  <?php endforeach; ?>
                </select>
              </div>
              <div class="form-group">
                <label>Enter or choose price</label>
                <input type="number" id="price" class="form-control"  name="price_old" required>
              </div>
                <input type="hidden" name="price" class="form-control" id="mainPrice"required>
             <div class="form-group">
                <input type="file" name="image" id="image" class="form-control" required  accept="image/*" onChange="validateImage(this.value)">
              </div>
              <button type="submit" name="submit" class=" form-control btn  btn-success">Add</button>
            </form>
          </form>
          </div>
        </div>
      </div>
    </div>

1 个答案:

答案 0 :(得分:0)

我认为您的代码可能会遇到一些问题,但我们首先关注您提到的问题/错误。在控制器功能中,您没有将帖子传递给模型。你需要在控制器中做这样的事情:

$addProduct = $this->product_model->addProduct($_POST);

if($addProduct){
    $upload_data = $this->upload->data();
    $this->session->set_flashdata('message', 'A new product,'. $upload_data['file_name'].'has been added!');
    redirect('dashboard');
}

在模型函数中,您需要首先让函数知道有一个传入参数,然后调整如何将字段分配给数据数组。

public function addProduct($post){
    $data = array(
        'category_id' =>    $post["category_id"],
        'title' =>          $post["title"],
        'description' =>    $post["description"],
        'price' =>          $post["price"]
    );

    return $this->db->insert('products',$data);
}

之后,我注意到你的模型中还有其他一些FILES逻辑。这也可能会出错。我认为必须在控制器中完成,而不是模型。在控制器中进行上载后,您也可以将文件名传递给模型。

$upload_data = $this->upload->data();
$addProduct = $this->product_model->addProduct($_POST, $upload_data['file_name']);

if($addProduct){
    $this->session->set_flashdata('message', 'A new product,'. $upload_data['file_name'].'has been added!');
    redirect('dashboard');
}

然后,调整模型。

public function addProduct($post, $fileName){
    $data = array(
        'category_id' =>    $post["category_id"],
        'title' =>          $post["title"],
        'description' =>    $post["description"],
        'price' =>          $post["price"],
        'image' =>          $fileName
    );

    return $this->db->insert('products',$data);
}

希望这有帮助。