EllisLab text mark
Advanced Search
     
handle duplicate entry tips
Posted: 28 January 2010 08:48 AM   [ Ignore ]
Avatar
Joined: 2010-01-08
79 posts

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

here’s my controller 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

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

A Database Error Occurred

Error Number
1062

Duplicate entry 
'testing' for key 'PRIMARY'

INSERT INTO `user` (`email`, `username`, `passwd`) VALUES ('test@gmail.com''testing''a4cac82164ef67d9d07d379b5d5d8c4abe1exxxxx'
 Signature 

oh yeah.time flies….

 
Posted: 28 January 2010 09:23 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2009-11-01
296 posts

do a check before inserting if the primary key exists

$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 
 Signature 

http://flakron.net

 
Posted: 28 January 2010 10:47 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2010-01-08
79 posts

cool, thanks for the idea. i altered my model function

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

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

now it works, awesome smile

 Signature 

oh yeah.time flies….

 
Posted: 28 January 2010 11:07 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2009-07-06
50 posts

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/

 
Posted: 28 January 2010 11:13 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2010-01-08
79 posts

great. thanks. am now subscribed to the topics am having trouble with specially these ones that I started

 Signature 

oh yeah.time flies….

 
Posted: 06 October 2012 09:57 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2012-02-21
1 posts

Thanks , it was very useful :D