duanji5746 2017-04-11 13:22
浏览 39
已采纳

Codeigniter:如果记录存在则更新记录,或者插入新记录

In my Table i have four columns .

  • r_id
  • id (User id)
  • v_id (company id)
  • rate

All i am doing is to rate the company (v_id) from user.

Suppose, if user one is rate the 1st Company (v_id),Again when same user rate the same Company Then rate column automatically updated. if user one wants to rate another company then a new row will be added in the table. But in my case when same user again rate the company then new row is inserted in the table.

Model

I don't know where i am doing wrong in this Model.

function ratings($insert,$id,$update,$v_id) {
            // $this->db->where('id',$id);
            // $this->db->where('v_id',$v_id);
            // $run = $this->db->get('user_ratings');
            // if ($run->num_rows() >= 1) {
            //  $this->db->where('id',$id);
           // $this->db->update('user_ratings',$update);

        // }else {
        //  // $this->db->set('user_id', $id);
         // $this->db->insert('user_ratings',$insert);
        // }

        $query = $this->db->query('select id, v_id from user_ratings where id = "'.$id.'" and v_id = "'.$v_id.'"')->num_rows();

        if ($query > 0) {
            $this->db->query('update user_ratings set rate ="'.$update.'" where id = "'.$id.'"');
        }else {
            $this->db->insert('user_ratings',$insert);
        }
    } 

Controller

function ratings() {
            $id = $this->session->userdata('id');
            $v_id = $this->uri->segment(3);
            $insert= array (
                'rate' => $this->input->post('click_val'),
                'date' => date('Y-m-d H:i:s'),
                'v_id' => $this->input->post('company_id'),
                'id' => $this->input->post('id')
                );
            $update = array (
                'rate' => $this->input->post('click_val'),
                'date' => date('Y-m-d H:i:s'),

                );
            $this->Visa_mdl->ratings($value,$id,$update,$v_id);
        }
  • 写回答

2条回答 默认 最新

  • douxianxing5712 2017-04-11 13:53
    关注

    Your on the right track. These simple changes should do the job.

    Model

    function ratings($data, $id, $v_id)
    {
        $query = $this->db->query("select r_id from user_ratings where id = $id and v_id = $v_id");
        if($query->num_rows() > 0)
        {
            $data['r_id'] = $query->row()->r_id;
            return $this->db
                ->where('id', $id)
                ->where('v_id', $v_id)
                ->update('user_ratings', $data);
        }
        else
        {
            return $this->db->insert('user_ratings', $data);
        }
    }
    

    The method returns true or false to indicate the outcome.

    Your controller modified to work with the above

    function ratings()
    {
        $id = $this->session->userdata('id');
        $v_id = $this->input->post('company_id');
    
        $data = array(
          'rate' => $this->input->post('click_val'),
          'date' => date('Y-m-d H:i:s'),
          'v_id' => $v_id,
          'id' => $id
        );
        $this->Visa_mdl->ratings($data, $id, $v_id);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!