Jerielle
Jerielle

Reputation: 7520

How to get if the inserted data in table is successful or failed using CodeIgniter?

How can I validate if the data is successful or not using CodeIgniter? I search the codeigniter manual I found out that getting num_rows() and affected_rows() can help me but how to use this for validation purposes? The example in the web is using SELECT statement and not INSERT or UPDATE. Here's a bit of my code.

public function create_user() {

        $username = $this->input->post('username');
        $password = md5($this->input->post('password'));
        $lastname = ucwords($this->input->post('lastname'));
        $firstname = ucwords($this->input->post('firstname'));

        $email = $this->input->post('email');
        $phone1 = $this->input->post('tel1');
        $phone2 = $this->input->post('tel2');

        $c_firstname = ucwords($this->input->post('c_firstname'));
        $c_lastname = ucwords($this->input->post('c_lastname'));
        $c_bldg = $this->input->post('c_bldg');
        $c_address = $this->input->post('c_address');
        $c_barangay = $this->input->post('c_barangay');
        $c_city = $this->input->post('c_city');
        $c_state = $this->input->post('c_state');
        $c_country = $this->input->post('c_country');
        $c_postal = $this->input->post('c_postal');
        $c_tel = $this->input->post('c_tel');

        $date = strtotime(date('Y-m-d H:i:s'));

        $insert_user = array(
            'user_login' => $username,
            'timestamp' => $date,
            'password' => $password,
            'firstname' => $firstname,
            'lastname' => $lastname,
            'email' => $email,
            'phone' => $phone1
        );

        $insert_user_data = $this->db->insert('cscart_users',$insert_user);

        //validate if data is inserted here. If inserted successfully proceed to next INSERT.

        $get_id = $this->db->insert_id();

        $insert_user_profile = array(
            'user_id' => $get_id,
            'b_firstname' => $c_firstname,
            'b_lastname' => $c_lastname,
            'b_address' => $c_address,
            'b_state' => $c_state,
            'b_city' => $c_city,
            'b_country' => $c_country,
            'b_zipcode' => $c_postal,
            'b_phone' => $c_tel
        );

        $this->db->insert('cscart_user_profiles',$insert_user_profile);

    }

Upvotes: 0

Views: 405

Answers (1)

Jerric Calosor
Jerric Calosor

Reputation: 36

I assume that your tble has auto inc set in your prmary key, codeigniter's function insert_id returns an int value if I recall it correctly; 0 if insert is unsuccessful and of coarse the incremented id if it is, so it's safe to say that you could use those value instead.

 //validate if data is inserted here. If inserted successfully proceed to next INSERT.



     $get_id = $this->db->insert_id();

     //check the value if it is 0 or not
     if($get_id > 0){

        $insert_user_profile = array(
            'user_id' => $get_id,
            'b_firstname' => $c_firstname,
            'b_lastname' => $c_lastname,
            'b_address' => $c_address,
            'b_state' => $c_state,
            'b_city' => $c_city,
            'b_country' => $c_country,
            'b_zipcode' => $c_postal,
            'b_phone' => $c_tel
        );

        $this->db->insert('cscart_user_profiles',$insert_user_profile);
   }

Upvotes: 2

Related Questions