2016-12-21 8 views
1

codeigniterを使用してフォームを送信中にこのエラーが発生しました。私はドキュメンテーションに従っていても助けが必要です。 私はこれは私のモデルのコードcodeigniterを使用してデータベースに新しい項目を追加する際にエラーが発生しました

//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'); 
    } 
} 
} 
} 
} 

そして、私のビュー

のコードです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 

の2.xバージョンを使用しています

<!-- 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> 
+0

ここで、製品モデルをロードしていますか?それは自動ロードですか、コントローラの構成ですか?また、実際にはaddProduct()のモデル関数にポストを渡すことはありません。 – cfnerd

+0

同じ機能を使用して、ビューをトリガーしてデータを受信しますか? –

+0

@cfnerd、私はオートロードを使用します。私はまだ初心者ですが、それを使って何かを作ろうとしていて、エラーのために立ち往生していました。 –

答えて

0

Iあなたのコードでいくつかの問題が起こっているかもしれないと思っていますが、最初に言及したあなたの質問/エラーに集中しましょう。あなたのコントローラー関数では、ポストをモデルに渡していません。あなたは、コントローラにこのような何かを実行する必要があります。

$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); 
} 

、私はあなたのモデルにいくつかの他のファイルのロジックを持っていることに気づきました。それも間違いの可能性があります。私はモデルではなくコントローラで行う必要があると考えています。コントローラでアップロードを行うと、ファイル名をモデルに渡すこともできます。

$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); 
} 

これが役立ちます。

関連する問題