douchuang4181 2015-07-16 01:53
浏览 50

使用PHP和CodeIgniter在数据库中对价目表进行折扣

I have a view with a form to input a percentage of discount/rise and apply it to a list of products stored in a table of a database, here is the code of the controller which I explain below:

    $rise = 1+$this->input->post('rise')/100;
    $this->db->where('client',$this->session->userdata('id_client'));
    $query = $this->db->get('products');

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

        $price = array(
            'price' => $row->price*$rise
        );
        $this->db->where('id',$row->id);
        $this->db->update('products',$price);

    }

    redirect('site/select_client');

First set the rise, then get from the products table all the ones that correspond to the client that we are working on (stored as session variable), an for each one set the array to update and update in for the product that has the corresponding id. (the table has the columns id, client, name and price). After that it should redirect to another controller.

It shows no error but no changes are made on the database.

  • 写回答

1条回答 默认 最新

  • duanhuhong5255 2015-07-16 04:37
    关注
    I modified your code, please use this and check. it will work correctly.
    $rise = $this->input->post('rise')/100;
    $this->db->where('client',$this->session->userdata('id_client'));
    $query = $this->db->get('products');
    
    foreach ($query->result() as $row){
    
          $price=$row->price + ($row->price * $rise);
    
        $price = array(
            'price' => $price
        );
        $this->db->where('id',$row->id);
        $this->db->update('products',$price);
    
    }
    
    评论

报告相同问题?