无法更新产品表获取必须使用“set”方法更新条目

时间:2015-01-16 14:17:05

标签: php mysql codeigniter

我还在学习codeigniter并且遇到了问题。那是 您必须使用“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');

if($ 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>

1 个答案:

答案 0 :(得分:0)

在某些情况下,如果在数组中, codeigniter 更新/插入需要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';

如果需要更新产品,则需要在

上设置名为edit的功能

模型获取

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

控制器 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>