![]() |
To do a query before inserting data in mySQL - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: To do a query before inserting data in mySQL (/showthread.php?tid=12844) Pages:
1
2
|
To do a query before inserting data in mySQL - El Forum - 11-02-2008 [eluser]ambf[/eluser] Hi! I'm new here and need help ASAP! I have a form and need to save it in the database. (Works fine!) But before to save the new data in the database I need to run a query in the database to see if the email inserted at the form is already registered in the database. How should I do the query?? here is my controller: <?php class Form extends Controller { function form() { parent::Controller(); } function display() { $data['query'] = $this->db->get('contact'); $this->load->view('display', $data); } function index() { $this->load->helper(array('form', 'url')); $this->load->library('validation'); $rules['field_01'] = "required"; $rules['field_02'] = "required"; $rules['field_03'] = "required"; $rules['field_04'] = "required|valid_email"; $rules['field_05'] = "required|max_length[2]"; $rules['field_06'] = "required"; $rules['field_07'] = "required"; $this->validation->set_rules($rules); if ($this->validation->run() == FALSE) { $this->load->view('myform'); } else { $this->db->insert('contact', $_POST); $this->load->view('formsuccess', $_POST); } } } ?> To do a query before inserting data in mySQL - El Forum - 11-02-2008 [eluser]ambf[/eluser] Pleaseeeee! To do a query before inserting data in mySQL - El Forum - 11-02-2008 [eluser]ray73864[/eluser] set the 'email' column in the table to be 'unique', that way when you try to insert it will fail and then you can test for that fail using 'affected_rows()', if it failed then send them back to the form with what the values they had previously entered. To do a query before inserting data in mySQL - El Forum - 11-02-2008 [eluser]ambf[/eluser] Ray! thanks! But how should i use affected_rows? and where should i place it? To do a query before inserting data in mySQL - El Forum - 11-02-2008 [eluser]ambf[/eluser] What did I do wrong??? Why does emailcheck() function not work or send me 'myform' when it receives a duplicate email????? Please help!!!!!!! <?php class Form extends Controller { function form() { parent::Controller(); } function display() { $this->db->from('contact'); $this->db->order_by("field_00", "Desc"); $data['query'] = $this->db->get(); $this->load->view('display', $data); } function index() { $this->load->helper(array('form', 'url')); $this->load->library('validation'); $rules['field_01'] = "required"; $rules['field_02'] = "required"; $rules['field_03'] = "required"; $rules['field_04'] = "required|valid_email|callback_emailcheck"; $rules['field_05'] = "required|max_length[2]"; $rules['field_06'] = "required"; $rules['field_07'] = "required"; $this->validation->set_rules($rules); if ($this->validation->run() == FALSE) { $this->load->view('myform'); } else { $this->db->insert('contact', $_POST); $this->load->view('formsuccess', $_POST); } } function emailcheck() { $query = $this->db->query('SELECT field_04 FROM contact'); $this->db->where('field_04', $field_04); if ($query->num_rows() > 0) { $this->load->view('myform'); } } } ?> To do a query before inserting data in mySQL - El Forum - 11-02-2008 [eluser]Mike Ryan[/eluser] You need to include the variable name (e.g. $email) in the function declaration: Code: function emailcheck($email) <-- HERE Also, usually the function should just return true or false, and let the controller handle what happens next: Code: function emailcheck($email) To do a query before inserting data in mySQL - El Forum - 11-02-2008 [eluser]ambf[/eluser] Mike! Thanks a lot! Is almoust working! Now I need to figure out how to send the visitor to the sucess page or to the form again.. To do a query before inserting data in mySQL - El Forum - 11-02-2008 [eluser]ambf[/eluser] Mike! I'm receiving this message because of this line... $this->form_validation->set_message('emailcheck', 'Email already registered'); Fatal error: Call to a member function on a non-object in /home/bridge/public_html/test12/CI_Bridge/system/application/controllers/form.php on line 62 What should I do? To do a query before inserting data in mySQL - El Forum - 11-02-2008 [eluser]ambf[/eluser] Please...? Someone can give me a light??? To do a query before inserting data in mySQL - El Forum - 11-02-2008 [eluser]Mike Ryan[/eluser] Oops, the code I posted is for 1.7.0. Looks like you are using a previous version. Try this: Code: $this->validation->set_message(‘emailcheck’, ‘Email already registered’); |