php - CodeIgniter: checking if record exists, tried everything -
i've been working on codeigniter project school , need create ticket user data. if user not yet in database, info filled form gets inserted database , if exists in database, ticket created existing user id.
i've been trying check if user exists e-mail, i've had no luck. googled hours; tried callbacks, normal validations, name it. can't seem work , appreciate help. here's 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 visitor 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 { homecoming false; } }
view (if @ 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 encountered
severity: notice
message: undefined variable: email
filename: controllers/booking.php
line number: 42
i don't know why happening because if don't set $email variable there, error he's missing argument i'm little confused here. also, main problem have that, despite giving errors , everything, inserts record database anyway... despite record same e-mail address beingness there!
i don't know how prepare this, help appreciated. in advance.
just 1 mistake, assing email field info variable, pass email add_ticket
model. thats :)
$email = $this->input->post("email"); $this->ticket_model->add_ticket($email);
now modify model, little,
function add_ticket($email) { // added $this->db->escape() , limit 1 performance $query = $this->db->query("select email visitor email = ".$this->db->escape($email)." limit 1"); // collect info 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') ); // create code short , clear homecoming $query->num_rows() == 0 ? $this->db->insert('visitor', $data) : false; }
php codeigniter validation record exists
No comments:
Post a Comment