title: Check if user exists in database
body: <p>I've made a user class which validates the data passed through the form and then subsequently updates the database table users. I want to add extra functionality such as checking if the username and email exists in the table, I've added a little script however it doesn't seem to be working. </p>
  
  <p>I inserted a duplicated email address and I did not get the error message "email exists" instead I get the success message "1 row inserted": </p>
  
  <p>Am I doing something wrong below? Is there perhaps a better way to approach this?</p>
  
  <pre><code> public function insert() {
  
  if (isset($_POST['submit'])) {
      $email = isset($_POST['email']) ? $this-&gt;mysqli-&gt;real_escape_string($_POST['email']) : '';
  
  $result = $this-&gt;mysqli-&gt;prepare("SELECT * FROM users WHERE email='".$email."'");
  
  if ($result-&gt;num_rows) {
  echo "email exisits!";
  } 
   else
   {
  $stmt = $this-&gt;mysqli-&gt;prepare("INSERT INTO users (username, password, name, email) VALUES (?, ?, ?, ?)");
          $stmt-&gt;bind_param('ssss', $username, $password, $name, $email); // bind strings to the paramater
          //escape the POST data for added protection
  
  $username = isset($_POST['username']) ? $this-&gt;mysqli-&gt;real_escape_string($_POST['username']) : '';
  $cryptedPassword = crypt($_POST['password']);
  $password = $this-&gt;mysqli-&gt;real_escape_string($cryptedPassword);
      $name = isset($_POST['name']) ? $this-&gt;mysqli-&gt;real_escape_string($_POST['name']) : '';
      $email = isset($_POST['email']) ? $this-&gt;mysqli-&gt;real_escape_string($_POST['email']) : '';
          /* execute prepared statement */
  $stmt-&gt;execute();
      printf("%d Row inserted.\n", $stmt-&gt;affected_rows);
      /* close statement and connection */
  $stmt-&gt;close();
              }
  </code></pre>