2015-01-16 30 views
-1

即時通訊仍在學習codeigniter,並提出了一個問題。那是 您必須使用「set」方法更新條目。無法更新產品表獲取您必須使用「set」方法來更新條目

我想更新一個表:下面包含product_name,product_desc等欄的產品是我的控制器,視圖和模型..我無法弄清楚我做錯了什麼..我總是檢查出print_r($ array) ;它給了我一個空陣列...雖然我確實在鏈接上輸入字段中顯示產品詳細信息:ci/index.php/admin/product/1 ..

將視圖的一半代碼視爲視圖代碼是長..

控制器:

// ****爲編輯產品**** //

public function edit_products(){ 


        $data = array ('product_name'=>$this->input->post('product_name'), 
        'product_desc'=>$this->input->post('product_desc'), 
        'product_stock'=>$this->input->post('product_stock'), 
        'product_sku'=>$this->input->post('product_sku'), 
        'inventory_date'=>$this->input->post('inventory_date'), 
        'product_price'=>$this->input->post('product_price'), 
        'featured_product'=>$this->input->post('featured_product'), 
        'best_seller'=>$this->input->post('best_seller'), 
        'brand_id'=>$this->input->post('brand_id'), 
        ); 

        $upd_pr = $this->mainmodel->update_product($_POST); 
        if($upd_pr){ 
         $this->session->set_flashdata('messages', 'Updated Sucessfully'); 


         redirect('admin/editing_products'); 

          }else{ 
         $this->session->set_flashdata('error', 'Updation Failed'); 
          redirect('admin/editing_products'); 
           } 
      } 



    public function editing_products(){ 


$data['product']=$this->mainmodel->set_pro2(); 
     //$data['images'] = $this->products->images($id); 
     $data['content']='admin/edit_products'; 

     $this->load->view('admin/main',$data);  
      } 

型號:

/*for update Product*/ 
    public function update_product($array){ 

     extract($array); 


     //print_r($array); 
     //die; 

    $this->db->update('product',array('product_name'=>$product_name,'product_desc'=>$product_desc,'inventory_date'=>$inventory_date,'product_price'=>$product_price,'featured_product'=>$featured_product,'best_seller'=>$best_seller,'brand_id'=>$brand_id)); 

$this->db->update('product'); 



        return ; 
    } 


function set_pro2(){ 
     $q = $this->db->query('SELECT * FROM product,product_image,category_product WHERE product.product_id = category_product.product_id AND product.product_id = 1 LIMIT 1'); 

如果($ Q-> NUM_ROWS()> 0){

 foreach($q->result() as $row){ 

     $data[] = $row; 

     } 
     return $data; 
     } 
} 

查看:

<form class="form-horizontal form-row-seperated" enctype="multipart/form-data" action="<?php echo site_url('admin/edit_products') ?>" method="post"> 

<div class="form-group"> 
        <?php foreach($product as $pro){ ?> 
                <label class="col-md-2 control-label">Name:<span class="required"> 
                * </span> 
                </label> 
                <div class="col-md-10"> 
                 <input type="text" class="form-control" name="product_name" placeholder="" value="<?php echo $pro->product_name; ?>"> 
                </div> 
               </div> 
               <div class="form-group"> 
                <label class="col-md-2 control-label">Description: <span class="required"> 
                * </span> 
                </label> 
                <div class="col-md-10"> 
                 <input type="text" class="form-control" name="product_desc" value="<?php echo $pro->product_desc; ?>"> 
                </div> 
               </div> 
+0

在您的模型中,在update_product函數中,您有2個更新函數。刪除第二個,看看是否有效... – Craig

+0

謝謝非常我刪除了第二次更新,它的工作..但它是更新所有的產品..但不是正在被選中的... –

+0

你'必須設置WHERE。 $ this-> db-> where('product_name',$ array ['product_name']);假設產品名稱是唯一的。 – Craig

回答

0

在某些情況下更新/插入要求this->db->set如果在數組中。我也不建議將兩個db->update放置在您添加到模型上的同一張桌子上。

活躍的唱片指南http://www.codeigniter.com/user_guide/database/active_record.html

更新

// $brand_id = 0, $data are from the edit function on controller 
public function name($brand_id = 0, $data) { 
$product_name = $this-input->post('product_name'); 
$product_desc = $this-input->post('product_desc'); 
$inventory_date = $this-input->post('inventory_date'); 
$product_price = $this-input->post('product_price'); 
$featured_product = $this-input->post('featured_product'); 
$best_seller = $this-input->post('best_seller'); 

$data = array(
'product_name'=> $product_name, 
'product_desc'=> $product_desc, 
'inventory_date'=>$inventory_date, 
'product_price'=>$product_price, 
'featured_product'=> $featured_product, 
'best_seller'=> $best_seller, 
'brand_id'=> $this->uri->segment(what_ever) 
); 
$this->db->set($data); 
$this->db->update('product'); 
} 

或插入

$data = array(
'product_name'=> $product_name, 
'product_desc'=> $product_desc, 
'inventory_date'=>$inventory_date, 
'product_price'=>$product_price, 
'featured_product'=> $featured_product, 
'best_seller'=> $best_seller, 
'brand_id'=>$brand_id 
); 
$this->db->set($data); 
$this->db->insert_id(); 
$this->db->insert('product'); 

帖子更新

路由集$route['function/edit/(:any)'] = 'folder/products_list/edit/$1';

如果需要更新您需要設置一個叫做編輯功能的

型號產品獲取URI段

public function model_get_products() { 
return $this->db->get($this->db->dbprefix . 'products')->result_array(); 
} 

控制器幫助http://www.codeigniter.com/user_guide/libraries/uri.html

public function edit() { 
    Form validation 
    $this->load->library('form_validation'); 

    $this->form_validation->set_rules('what ever'); 

    if ($this->form->validation->run() == TRUE) { 
    $this->load->model('folder/model_update_product'); 

    //Example uri segment admin/product_list/edit/id_number $this->uri->segment('4') 

    $this->model_update_product->name($this->uri->segment('what_ever_is'), $this->input->post()) 

    redirect('function/product_list'); 

    } else { 
    // Load Your Form View i.e. $this->get_form(); 
    } 
} 

public function index() { 

$this->load->model('folder/model_get_products'); 

$results = $this->model_get_products->get(); 

$data['products'] = array(); 

//Example uri segment admin/product_list/edit/id_number $this->uri->segment('4') 

// Add What Else You Need to here and then you can add it to view 

foreach($results as $result) { 
    $data['products'][] = array(
    'brand_id' => $result[brand_id], 
    'edit' => site_url('controller_name/edit'). '/' .$result[brand_id] // site_url must match what you set in routes for edit. 
    ); 
} 

return $this->load->view('folder/products_list', $data); 
} 

public function get_form() { 
//Example uri segment admin/product_list/edit/id_number $this->uri->segment('4') 

// Make sure you set another model function in your get model where can get by id. 

$product_info = $this->get_products_by_id->get($this->uri->segment('4')); 

    $product_name = $this->input->post('product_name'); 

    if (isset($product_name)) { 
    $data['product_name'] = $product_name; // for name="" in input 
    } elseif (!empty($product_info)) { 
    $data['product_name'] = $product_info['product_name']; // for value="" in input 
    } else { 
    $data['product_name'] = ''; 
    } 

    $this->load->view('folder/product_form', $data); 

product form contents 
} 

查看產品列表

<form> 
<table> 
<thead> 
</thead> 
<tbody> 
<?php if ($products) { ?> 
<?php foreach ($products as $product) { ?> 
<tr> 
<td><?php echo $product['brand_id'];?></td> 
<td><a href="<?php echo $edit;?>"> Edit Product</a></td> 
</tr> 
<?php } ?> 
<?php } ?> 
</tbody> 
</table> 
</form> 
+0

三江源非常多我刪除了第二次更新和它的工作..但它正在更新所有的產品..但不是其被選擇的一個......「\t公共職能update_product($陣列){ \t \t \t \t \t \t extract($ array); \t \t $ this-> db-> update('product',array('product_name'=> $ product_name,'product_desc'=> $ product_desc,'inventory_date'=> $ inventory_date,'product_price'=> $ PRODUCT_PRICE, 'featured_product'=> $ featured_product, 'best_seller'=> $ best_seller, 'brand_id'=> $ brand_id)); \t \t \t \t \t \t \t \t返回$這 - > DB-> get_where( '產品',陣列( 'PRODUCT_ID'=> $ USER_ID)) - >結果(); ; \t \t} \t \t「 –

+0

三江源我檢查你的代碼,它會得到我需要什麼,但我也嘗試另一種辦法解決,這是通過在表單傳遞一個隱藏的id字段,然後得到它的功能。所以這給了我產品的id現在它只更新一個產品,而不是全部:)... Thankyou所有的幫助我:) –

+0

你必須設置一個複選框和許多其他功能。一起更新所有產品相當複雜 – user4419336