Search code examples
phpcodeignitervalidationrecordexists

CodeIgniter: checking if record exists


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.

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:

Controller

public function create_ticket() {
    $this->load->library('form_validation');
    // field name, error message, validation rules
    $this->form_validation->set_rules('voornaam', 'Voornaam', 'trim|required');
    $this->form_validation->set_rules('tussenvoegsel', 'Tussenvoegsel', 'trim');
    $this->form_validation->set_rules('achternaam', 'Achternaam', 'trim|required');
    $this->form_validation->set_rules('email', 'E-mailadres', 'trim|required|valid_email');
    $this->form_validation->set_rules('geboortedatum', 'Geboortedatum', 'required');
    $this->form_validation->set_rules('postcode', 'Postcode', 'trim|required');
    $this->form_validation->set_rules('woonplaats', 'Woonplaats', 'trim|required');

    if($this->form_validation->run() == FALSE)
    {
        echo "Failed";
        $this->reservate();
    }
    else
    {
        $this->ticket_model->add_ticket($email);
        $this->reservate();

    }
}

Model:

public function add_ticket($email) {
    $query = $this->db->query("SELECT email FROM visitor
                               WHERE email = '".$email."'");
    if($query->num_rows() == 0){
        $data = array(
            'voornaam' => $this->input->post('voornaam'),
            'tussenvoegsel' => $this->input->post('tussenvoegsel'),
            'achternaam' => $this->input->post('achternaam'),
            'email' => $this->input->post('email'),
            'geboortedatum' => $this->input->post('geboortedatum'),
            'postcode' => $this->input->post('postcode'),
            'woonplaats' => $this->input->post('woonplaats')
        );
        $this->db->insert('visitor', $data);
    }
    else {
        return false;
    }

    }

View (if at all important)

<?php echo validation_errors(); ?>
<h1>Ticket Bestellen</h1>
<label>
    <span>Voornaam</span>
    <input type="text" class="input_text" name="voornaam" id="voornaam"/>
</label>
<label>
    <span>Tussenvoegsel</span>
    <input type="text" class="input_text" name="tussenvoegsel" id="tussenvoegsel"/>
</label>
<label>
    <span>Achternaam</span>
    <input type="text" class="input_text" name="achternaam" id="achternaam"/>
</label>
<label>
    <span>E-mailadres</span>
    <input type="text" class="input_text" name="email" id="email"/>
</label>
<label>
    <span>Geboortedatum</span>
    <input type="text" id="geboortedatum" name="geboortedatum" placeholder="dd-mm-jjjj">
</label>
<label>
    <span>Postcode</span>
    <input type="text" class="input_text" name="postcode" id="postcode"/>
</label>
<label>
    <span>Woonplaats</span>
    <input type="text" class="input_text" name="woonplaats" id="woonplaats"/>
</label>
    <input type="submit" class="button" value="Reserveren" />
</label>

The error I'm getting is:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: email

Filename: controllers/booking.php

Line Number: 42

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!

I really don't know how to fix this, so any help is greatly appreciated. Thanks in advance.


Solution

  • Just one mistake, Assing Email field data to a variable, then pass your email to the add_ticket model. Thats it :)

    $email = $this->input->post("email");
    $this->ticket_model->add_ticket($email);
    

    Now Modify Your Model, Just a little,

    function add_ticket($email) {
    
        // Added $this->db->escape() and limit 1 for Performance
        $query = $this->db->query("SELECT email FROM visitor
                               WHERE email = ".$this->db->escape($email)." limit 1");
    
       // Collect Data Array
       $data = array(
            'voornaam' => $this->input->post('voornaam'),
            'tussenvoegsel' => $this->input->post('tussenvoegsel'),
            'achternaam' => $this->input->post('achternaam'),
            'email' => $this->input->post('email'),
            'geboortedatum' => $this->input->post('geboortedatum'),
            'postcode' => $this->input->post('postcode'),
            'woonplaats' => $this->input->post('woonplaats')
        );
    
     // Make Code Short and Clear
     return $query->num_rows() == 0 ? $this->db->insert('visitor', $data) : false;
    }