[eluser]phpserver[/eluser]
I want to check whether an email that has been submitted is already in my database.Here is my contoller::
form.php
Code:
<?php
class Form extends Controller {
function Form()
{
parent::Controller();
$this->load->model('arc_model');
}
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('Validation');
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|unique[mytable.username]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|unique[mytable.email]|valid_email');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->arc_model->insert($this->input->post('username'), $this->input->post('email'));
redirect('form/success');
}
}
function success()
{
echo "Your data has been securely submitted and is now in our safe custody";
}
}
?>
Here is MY_Validation.php class
MY_Validation.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* MY_Validation Class
*
* Extends Validation library
*
* Adds one validation rule, "unique" and accepts a
* parameter, the name of the table and column that
* you are checking, specified in the forum table.column
*/
class MY_Validation extends CI_Validation {
function My_Validation()
{
parent::CI_Validation();
}
// --------------------------------------------------------------------
/**
* Unique
*
* @access public
* @param string
* @param field
* @return bool
*/
function unique($str, $field)
{
$CI =& get_instance();
list($table, $column) = split("\.", $field, 2);
$CI->validation->set_message('unique', 'The %s that you requested is unavailable.');
$query = $CI->db->query("SELECT COUNT(*) dupe FROM $table WHERE $column = '$str'");
$row = $query->row();
return ($row->dupe > 0) ? FALSE : TRUE;
}
}
?>