title: CodeIgniter: checking if record exists
body: <p>I've been working on a CodeIgniter project for school and I need to create a ticket with user data. If the user is not yet in the database, the data he filled into the form gets inserted into the database and if he already exists in the database, just a ticket is created with his already existing user id.</p>
  
  <p>I've been trying to check if a user exists with his e-mail, but I've had no luck. Googled for hours; tried callbacks, normal validations, you name it. I just can't seem to get it to work and would really appreciate some help. Here's my current code:</p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public function create_ticket() {
      $this-&gt;load-&gt;library('form_validation');
      // field name, error message, validation rules
      $this-&gt;form_validation-&gt;set_rules('voornaam', 'Voornaam', 'trim|required');
      $this-&gt;form_validation-&gt;set_rules('tussenvoegsel', 'Tussenvoegsel', 'trim');
      $this-&gt;form_validation-&gt;set_rules('achternaam', 'Achternaam', 'trim|required');
      $this-&gt;form_validation-&gt;set_rules('email', 'E-mailadres', 'trim|required|valid_email');
      $this-&gt;form_validation-&gt;set_rules('geboortedatum', 'Geboortedatum', 'required');
      $this-&gt;form_validation-&gt;set_rules('postcode', 'Postcode', 'trim|required');
      $this-&gt;form_validation-&gt;set_rules('woonplaats', 'Woonplaats', 'trim|required');
  
      if($this-&gt;form_validation-&gt;run() == FALSE)
      {
          echo "Failed";
          $this-&gt;reservate();
      }
      else
      {
          $this-&gt;ticket_model-&gt;add_ticket($email);
          $this-&gt;reservate();
  
      }
  }
  </code></pre>
  
  <p><strong>Model:</strong></p>
  
  <pre><code>public function add_ticket($email) {
      $query = $this-&gt;db-&gt;query("SELECT email FROM visitor
                                 WHERE email = '".$email."'");
      if($query-&gt;num_rows() == 0){
          $data = array(
              'voornaam' =&gt; $this-&gt;input-&gt;post('voornaam'),
              'tussenvoegsel' =&gt; $this-&gt;input-&gt;post('tussenvoegsel'),
              'achternaam' =&gt; $this-&gt;input-&gt;post('achternaam'),
              'email' =&gt; $this-&gt;input-&gt;post('email'),
              'geboortedatum' =&gt; $this-&gt;input-&gt;post('geboortedatum'),
              'postcode' =&gt; $this-&gt;input-&gt;post('postcode'),
              'woonplaats' =&gt; $this-&gt;input-&gt;post('woonplaats')
          );
          $this-&gt;db-&gt;insert('visitor', $data);
      }
      else {
          return false;
      }
  
      }
  </code></pre>
  
  <p><strong>View (if at all important)</strong></p>
  
  <pre><code>&lt;?php echo validation_errors(); ?&gt;
  </code></pre>
  
  
  
  <pre><code>&lt;h1&gt;Ticket Bestellen&lt;/h1&gt;
  &lt;label&gt;
      &lt;span&gt;Voornaam&lt;/span&gt;
      &lt;input type="text" class="input_text" name="voornaam" id="voornaam"/&gt;
  &lt;/label&gt;
  &lt;label&gt;
      &lt;span&gt;Tussenvoegsel&lt;/span&gt;
      &lt;input type="text" class="input_text" name="tussenvoegsel" id="tussenvoegsel"/&gt;
  &lt;/label&gt;
  &lt;label&gt;
      &lt;span&gt;Achternaam&lt;/span&gt;
      &lt;input type="text" class="input_text" name="achternaam" id="achternaam"/&gt;
  &lt;/label&gt;
  &lt;label&gt;
      &lt;span&gt;E-mailadres&lt;/span&gt;
      &lt;input type="text" class="input_text" name="email" id="email"/&gt;
  &lt;/label&gt;
  &lt;label&gt;
      &lt;span&gt;Geboortedatum&lt;/span&gt;
      &lt;input type="text" id="geboortedatum" name="geboortedatum" placeholder="dd-mm-jjjj"&gt;
  &lt;/label&gt;
  &lt;label&gt;
      &lt;span&gt;Postcode&lt;/span&gt;
      &lt;input type="text" class="input_text" name="postcode" id="postcode"/&gt;
  &lt;/label&gt;
  &lt;label&gt;
      &lt;span&gt;Woonplaats&lt;/span&gt;
      &lt;input type="text" class="input_text" name="woonplaats" id="woonplaats"/&gt;
  &lt;/label&gt;
      &lt;input type="submit" class="button" value="Reserveren" /&gt;
  &lt;/label&gt;
  </code></pre>
  
  
  
  <p>The error I'm getting is: </p>
  
  <pre><code>A PHP Error was encountered
  </code></pre>
  
  <p>Severity: Notice</p>
  
  <p>Message: Undefined variable: email</p>
  
  <p>Filename: controllers/booking.php</p>
  
  <p>Line Number: 42</p>
  
  <p>I don't know why this is happening because if I don't put the $email variable there, I get the error that he's missing an argument so I'm a little confused here. Also, the main problem I have is that, despite it giving errors and everything, he inserts the record into the database anyway... Despite a record with the same e-mail address already being there!</p>
  
  <p>I really don't know how to fix this, so any help is greatly appreciated. Thanks in advance.</p>