Welcome Guest, Not a member yet? Register   Sign In
To do a query before inserting data in mySQL
#1

[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);
}
}
}
?>
#2

[eluser]ambf[/eluser]
Pleaseeeee!
#3

[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.
#4

[eluser]ambf[/eluser]
Ray!

thanks! But how should i use affected_rows? and where should i place it?
#5

[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');
}
}



}
?>
#6

[eluser]Mike Ryan[/eluser]
You need to include the variable name (e.g. $email) in the function declaration:

Code:
function emailcheck($email) <-- HERE
  {
  $query = $this->db->query(‘SELECT field_04 FROM contact’);
  $this->db->where(‘field_04’, $email); <-- AND HERE
  if ($query->num_rows() > 0)
  {
  $this->load->view(‘myform’);
  }
  }

Also, usually the function should just return true or false, and let the controller handle what happens next:

Code:
function emailcheck($email)
  {
    $query = $this->db->query(‘SELECT field_04 FROM contact’);
    $this->db->where(‘field_04’, $email);
    if ($query->num_rows() == 0)
    {
        return true;
    else
    {
        $this->form_validation->set_message('emailcheck', 'Email already registered');      
        return false;
    }
  }
#7

[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..
#8

[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?
#9

[eluser]ambf[/eluser]
Please...? Someone can give me a light???
#10

[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’);




Theme © iAndrew 2016 - Forum software by © MyBB