Welcome Guest, Not a member yet? Register   Sign In
handle duplicate entry tips
#1

[eluser]sasori[/eluser]
Hi, can you guys give me tips or code snippets how handle the attempted duplicate entries in db ?

here's my controller code

Code:
public function create_member()
  {
    $this->load->library('form_validation');
    $this->form_validation->set_rules('email','Email Address','trim|required|valid_email');
    $this->form_validation->set_rules('username','Username','trim|required|min_length[6]|max_length[16]');
    $this->form_validation->set_rules('passwd','Password','trim|required|min_length[6]|max_length[16]');
    $this->form_validation->set_rules('passwd2','Password Confirm','trim|required|matches[passwd]');
    if($this->form_validation->run() == FALSE )
    {
        $data['title'] = 'Register';
        $data['main_content'] = "register_view";
        $this->load->view('includes/template',$data);
    }
    else
    {
      $this->load->model('member_model');
      if($query = $this->member_model->create_member())
      {
          $data['title'] = 'Welcome';
          $data['main_content'] = 'successful_view';
          $this->load->view('includes/template',$data);
      }
      else
      {
        $data['title'] = 'please register!';
        $data['main_content'] = 'register_view';
        $this->load->view('includes/template',$data);
      }
    }
  }

here's my model function
Code:
public function create_member()
  {
     $data = array(
                    'email' => $this->input->post('email'),
                    'username' => $this->input->post('username'),
                    'passwd' => sha1($this->input->post('passwd'))
     );
     $insert = $this->db->insert('user',$data);
     return $insert;
  }


here's the error am getting, it doesn't look good coz the table columns appear LOL

Code:
A Database Error Occurred

Error Number: 1062

Duplicate entry 'testing' for key 'PRIMARY'

INSERT INTO `user` (`email`, `username`, `passwd`) VALUES ('[email protected]', 'testing', 'a4cac82164ef67d9d07d379b5d5d8c4abe1exxxxx')
#2

[eluser]flaky[/eluser]
do a check before inserting if the primary key exists
Code:
$this->db->where('user', $this->input->post('username'));
$this->db->from('user');
$count = $this->db->count_all_results();

if($count == 0)
//do the insert
else
//reply with some error code or number
#3

[eluser]sasori[/eluser]
cool, thanks for the idea. i altered my model function
Code:
public function create_member()
  {
     $data = array(
                    'email' => $this->input->post('email'),
                    'username' => $this->input->post('username'),
                    'passwd' => sha1($this->input->post('passwd'))
     );

     $this->db->where('email',$this->input->post('email'));
     $q = $this->db->get('user');
     if($q->num_rows == 0)
     {
        $insert = $this->db->insert('user',$data);
     }
     else
     {
        return false;
     }

     return $insert;
  }

then i added this snippet to my view

Code:
if(isset($msg))
    {
        echo '<font color="red"><b>'.$msg.'</b></font><br /><br />';
    }

now it works, awesome Smile
#4

[eluser]Joshua Logsdon[/eluser]
Hi sasori, if you ever want more info or ways to go about it you can check out this recent thread:
http://ellislab.com/forums/viewthread/142811/
#5

[eluser]sasori[/eluser]
great. thanks. am now subscribed to the topics am having trouble with specially these ones that I started
#6

[eluser]Unknown[/eluser]
Thanks , it was very useful Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB