How to check if id already exists - codeigniter

前端 未结 7 1401
独厮守ぢ
独厮守ぢ 2021-02-09 06:56

i am trying to check if an id in the database already exists and if it does not then only insert that id and not the other ones that exist

I have tried to do a where sta

7条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-09 07:21

    Model

    load->database();
        }
    
        function check()
        {
            $query = null; //emptying in case 
    
            $id   = $_POST['id']; //getting from post value
            $name = $_POST['name'];
    
            $query = $this->db->get_where('fruits', array(//making selection
                'id' => $id
            ));
    
            $count = $query->num_rows(); //counting result from query
    
            if ($count === 0) {
                $data = array(
                    'name' => $name,
                    'id' => $id
                );
                $this->db->insert('fruits', $data);
            }
        }
    }
    
    ?>
    

提交回复
热议问题