How to check if id already exists - codeigniter

前端 未结 7 1400
独厮守ぢ
独厮守ぢ 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

    <?php
    class Fruits_model extends CI_Model
    {
        function __construct()
        {
            parent::__construct();
            $this->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);
            }
        }
    }
    
    ?>
    
    0 讨论(0)
  • 2021-02-09 07:21

    You have a logic issue with your code that you need to fix.

    In your code, you save the result from your query as $q = $this->db->get('testing'), and $q will always evaluate to true regardless of the number of rows your return.

    You need to check the number of rows using $query->num_rows() > 0 and then the rest of you code will behave as you expect.

    For more details, see: http://ellislab.com/codeigniter/user-guide/database/results.html

    0 讨论(0)
  • 2021-02-09 07:25

    normally 'id' field is set with auto_increment and set primary which is unique and not repeatable. So there is not problem to worry about existing.

    However, in your case I think you are not using it as a 'unique field'.

    Let me give you an example.

    Here I have a table name 'fruits'

    ++++++++++++++++++++++++++++++++++++
    ငfruit_id  | int (primary)
    name      | text 
    id        |  int
    ++++++++++++++++++++++++++++++++++++++
    

    in your model

    function checkId($id)
    {
       $query=$this->db->get_where('fruits',array('id'=>$id)); //check if 'id' field is existed or not
    
       if($query!=null)  // id found stop
       {
         return FALSE; 
       }
       else // id not found continue..
       {
           $data = array(
                 'fruit_id' => $fruit_id ,
                  'name' => $name ,
                 'id' => $id
            );    
          $this->db->insert('fruits', $data);          
       }    
    }
    
    0 讨论(0)
  • 2021-02-09 07:26

    You should try like this:

    public function record_exists(){
       $exists = $this->db->get_where('table_name', array('id' => $id));
       if($exists->num_rows() > 0 ){
           echo "Some message";
           return false;
       }else{
          // Insert your data into the database...
       }
    }
    
    0 讨论(0)
  • 2021-02-09 07:28

    For Checking An Id Or any column value exist not in database CI have a validation rule for it.

    See it live here: Validation Rule

    Rule: is_unique

    Returns FALSE if the form element is not unique to the table and field name in the parameter. Note: This rule requires Query Builder to be enabled in order to work.

    Example: is_unique[table.field]

    $this->form_validation->set_rules(
            'username', 'Username',
            'required|min_length[5]|max_length[12]|is_unique[users.username]',
            array(
                    'required'      => 'You have not provided %s.',
                    'is_unique'     => 'This %s already exists.'
            )
    );
    

    For More Advance use of validation, You can add all validation Setting Rules Using an Array.

            $this->form_validation->set_rules(
                'username', 'Username',
                'required|min_length[5]|max_length[12]|is_unique[users.username]',
                array(
                        'required'      => 'You have not provided %s.',
                        'is_unique'     => 'This %s already exists.'
                )
        );
    
    
        $config = array(
            'your_rule_name' => array(
                    array(
                            'username', 'Username',
                            'required|min_length[5]|max_length[12]|is_unique[users.username]',
                            array(
                                    'required'      => 'You have not provided %s.',
                                    'is_unique'     => 'This %s already exists.'
                            )
                    )
            ),        
            array(
                    'field' => 'email',
                    'label' => 'Email',
                    'rules' => 'required'
            )
    );
    
    $this->form_validation->set_rules($config);
    
    0 讨论(0)
  • 2021-02-09 07:31
    $ql = $this->db->select('id')->from('testing')->where('id',$id)->get();
    
    if( $ql->num_rows() > 0 ) {} else {
        $a = array('id' => $id, 'message' => $message);
        $this->db->insert('testing', $a);
    }
    

    This should do it.

    0 讨论(0)
提交回复
热议问题