AJAX HTML Javascript jQuery PHP Example MORE
Need a Website Or Web Application Or Any Help.Contact Us: +91 9437911966 (Whatsapp) Note: Paid Service
Students Tutorial app relased. Download Now.

How to Check Existing User in Database using PHP MVC with example


In this example we using Models, Views, Controller Structure for Checking Existing User in the database.

To Check Existing User in the database first we have to create a table.

register table

CREATE TABLE IF NOT EXISTS `register` (
`id` int(11) NOT NULL,
  `user_name` varchar(50) NOT NULL,
  `email_id` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Here is the model file which we are using for check user.

The file register_model.php is created under Models folder

models/register_model.php

<?php

class Register_Model extends Model
{
	public function __construct()
	{
		parent::__construct();
	}
	public function check_user($user_name,$email_id)
	{
		$result= $this->db->select("SELECT * FROM `register` WHERE user_name = '".$user_name."' OR email_id = '".$email_id."'");
		$count = count($result);
		return $count;
	}
	public function insert_user($data)
	{
		$this->db->insert('register', $data);
	}
}
?>

Here is the view file register.php which inside views folder contains the form

views/register/register.php

<!DOCTYPE html>
<html>
<body>
<form method="post" action="<?php echo URL; ?>register/check_user">
First name:<br>
<input type="text" name="user_name" >
<br>
Last name:<br>
<input type="text" name="email_id">
<br><br>
Password:<br>
<input type="password" name="password">
<br><br>
<input type="submit" value="Submit">
</form> 
</body>
</html>

Here is the Controller file register.php which inside controllers folder

controllers/register.php

<?php

class Register extends Controller
{
	public function __construct()
	{
		parent::__construct();
	}
function check_user(){
		$user_name=$_POST['user_name'];
		$email_id=$_POST['email_id'];
		$count=$this->model->check_user($user_name,$email_id);
					if($count > 0){
						echo 'This User Already Exists';
					}
					else{
		$data = array(
		'id' =>null,
		'user_name' =>$_POST['user_name'],
		'email_id' =>$_POST['email_id'],
		'password' =>$_POST['password']
		);
		$this->model->insert_user($data);
					}
header('location:register');
}
}
?>
Path: localhost/project_folder_name/view_folder_name/view_filename Example: localhost/mvc/register/register