title: Trapping Duplicate Entries in Register Data
body: <p>I have a registration form then this will what it read after i register a account in the form</p>
  <pre><code>   $('#manage-user').submit(function(e){
              e.preventDefault();
              start_loader()
              $.ajax({
                  url:_base_url_+'classes/Users.php?f=register',
                  data: new FormData($(this)[0]),
                  cache: false,
                  contentType: false,
                  processData: false,
                  method: 'POST',
                  type: 'POST',
                  success:function(resp){
                      if(resp ==1){
                alert('Registration Successful. You may now log-in.')
                          window.location.reload()
                      }else{
                          $('#msg').html('&lt;div class=&quot;alert alert-danger&quot;&gt;Email already exist&lt;/div&gt;')
                          end_loader()
                      }
                  }
              })
          })
  </code></pre>
  <hr />
  <p>This is my function.</p>
  <pre><code>public function register(){
          extract($_POST);
          $data = '';
          foreach($_POST as $k =&gt; $v){
              if(!in_array($k,array('id','password'))){
                  if(!empty($data)) $data .=&quot; , &quot;;
                  $data .= &quot; {$k} = '{$v}' &quot;;
              }
          }
  
  
      
          
          if(!empty($password)){
              $password = md5($password);
              if(!empty($data)) $data .=&quot; , &quot;;
              $data .= &quot; `password` = '{$password}' &quot;;
          }
  
          if(isset($_FILES['img']) &amp;&amp; $_FILES['img']['tmp_name'] != ''){
                  $fname = 'uploads/'.strtotime(date('y-m-d H:i')).'_'.$_FILES['img']['name'];
                  $move = move_uploaded_file($_FILES['img']['tmp_name'],'../'. $fname);
                  if($move){
                      $data .=&quot; , avatar = '{$fname}' &quot;;
                      if(isset($_SESSION['userdata']['avatar']) &amp;&amp; is_file('../'.$_SESSION['userdata']['avatar']))
                          unlink('../'.$_SESSION['userdata']['avatar']);
                  }
          }
          if(empty($id)){
              $data .= &quot; ,`type` = '2' &quot;;
              $qry = $this-&gt;conn-&gt;query(&quot;INSERT INTO users set {$data}&quot;);
              if($qry){
                  return 1;
              }else{
                  return 2;
              }
  
          }else{
              $qry = $this-&gt;conn-&gt;query(&quot;UPDATE users set $data where id = {$id}&quot;);
              if($qry){
                  $this-&gt;settings-&gt;set_flashdata('success','User Details successfully updated.');
                  foreach($_POST as $k =&gt; $v){
                      if($k != 'id'){
                          if(!empty($data)) $data .=&quot; , &quot;;
                          $this-&gt;settings-&gt;set_userdata($k,$v);
                      }
                  }
                  if(isset($fname) &amp;&amp; isset($move))
                  $this-&gt;settings-&gt;set_userdata('avatar',$fname);
  
                  return 1;
              }else{
                  return &quot;UPDATE users set $data where id = {$id}&quot;;
              }
              
          }
      }
  </code></pre>
  <hr />
  <p>When i add this line</p>
  <pre><code>        $user_check_query = &quot;SELECT * FROM users WHERE username='$username' LIMIT 1&quot;;
          $result = mysqli_query($db, $user_check_query);
          $user = mysqli_fetch_assoc($result);
  </code></pre>
  <p>It prompt Email already Exist but still create a account what do i need to add or change?
  im a beginner in coding so i dont really know what to do..</p>
  <p>Can anyone help me thank you</p>