Wednesday, 5 December 2018

PHP MVC User check

How to Check Existing User in Database using PHP MVC with an 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

PHP MVC Delete

How to Delete data in a database using PHP MVC with an example 

In this example, we using Models, Views, Controller Structure for Delete the inserted data.

To Delete data in the database first we have to create a Controller file.
controller/index.php
<?php
class Index extends Controller {
function __construct() {
parent::__construct();
}
function index() {
$this->view->allrecords = $this->model->getAllrecords();
$this->view->render('index/index');
}
function delete_index($id) 
{
$this->model->delete_index($id);
header('location: ../index/index');
}
}
Here is the model file which we are using for Delete data from database.
The file index_model.php is created under Models folder
models/index_model.php

index_model.php

<?php
class Index_Model extends Model
{
public function __construct()
{
parent::__construct();
}
public function getAllrecords()
{
return $this->db->select("SELECT * FROM `mvc` ORDER BY id DESC");
}
public function delete_index($id) 
{ $this->db->delete('mvc'"`id` = {$id}");
}
}
Here is the view file index.php which inside views folder contains the table
views/index/index.php
<table border="2" id="internalActivities" style="width:100%" class="table table-bordered">
<tr>
<th>Name</th>
<th>Email</th>
<th>contact</th>
<th></th>
</tr>
<?php foreach($this->allrecords AS $key=>$value){
?>

<tr>
<td><?php echo $value['name']."<br>"?></td>
<td><?php echo $value['email']."<br>"?></td>
<td><?php echo$value['contact'];?></a>
</td>
<td><a href="javascript:confirmDelete('delete_index/<?php echo $value['id'];?>')" class="btn btn-primary btn-sm" role="button">Delete</a></td>
<?php } ?>
</table>
<script>
function confirmDelete(delUrl) {
if (confirm("Are you sure you want to delete")) {
document.location = delUrl;
}
}
</script>
Path: localhost/project_folder_name/view_folder_name/view_filename
Example: localhost/mvc/index/index




PHP MVC View

How to View data in a database using PHP MVC with an example

In this example we using Models, Views, Controller Structure for View the inserted data.

To View data in the database first we have to create a Controller file.
controller/index.php
<?php
class Index extends Controller {
function __construct() {
parent::__construct();
}
function index() {
$this->view->allrecords = $this->model->getAllrecords();
$this->view->render('index/index');
}
function view_index() 
{
//Auth::handleLogin();$data = $_GET;
if($data['id']=='')
{
$this->view->pick='';
$this->view->data=$data;

else 
{
$this->view->pick=$data['id'];
$this->view->content= $this->model->viewOnerecord($data['id']);
}
$this->view->render('index/view_index');
}
}
Here is the model file which we are using for View data from database.
The file index_model.php is created under Models folder
models/index_model.php

index_model.php

<?php
class Index_Model extends Model
{
public function __construct()
{
parent::__construct();
}
public function getAllrecords()
{
return $this->db->select("SELECT * FROM `mvc` ORDER BY id DESC");
}
public function viewOnerecord($id)
{
return $this->db->select("SELECT * FROM `mvc` WHERE id='".$id."' LIMIT 1");
}
Here is the view file index.php which inside views folder contains the table
views/index/index.php
<table border="2" id="internalActivities" style="width:100%" class="table table-bordered">
<tbody>
<tr>
<th>id</th>
<td><?php echo $this->content[0]['id']; ?></td>
<th>name</th>
<td><?php echo $this->content[0]['name']; ?></td>
</tr>
<tr>
<th>email</th>
<td><?php echo $this->content[0]['email']; ?></td>
<th>contact</th>
<td><?php echo $this->content[0]['contact']; ?></td>
</tr>
</tbody>
</table>
Path: localhost/project_folder_name/view_folder_name/view_filename
Example: localhost/mvc/index/index


PHP MVC Update

How to Update data in database using PHP MVC with example

In this example we using Models, Views, Controller Structure for Update the inserted data.
To update data in the database first we have to create a Controller file.
controller/index.php

<?php
class Index extends Controller {
function __construct() {
parent::__construct();
}
function index() {
$this->view->allrecords = $this->model->getAllrecords();
$this->view->render('index/index');
}
function edit_index() 
{
$data = $_GET;
if($_GET['id']=='')
{
$this->view->pick='';
$this->view->data=$data;

else 
{
$this->view->pick=$_GET['id'];
$this->view->content= $this->model->getOnerecord($_GET['id']);
}
$this->view->render('index/edit_index');
}
function edit_submit_index(){
$arg=$_POST['id'];
$data = array(
'name' =>$_POST['name'],
'email' =>$_POST['email'],
'contact' => $_POST['contact']
);
$this->model->edit_submit_index($data,$arg);
}
header('location: index');
}
?>

Here is the model file which we are using for Updating the inserted data to database.
The file index_model.php is created under Models folder
models/index_model.php

<?php
class Index_Model extends Model
{
public function __construct()
{
parent::__construct();
}
public function getAllrecords()
{
return $this->db->select("SELECT * FROM `mvc` ORDER BY id DESC");
}
public function getOnerecord($id)
{
return $this->db->select("SELECT * FROM `mvc` WHERE id='".$id."' LIMIT 1");
}
public function edit_submit_index($data,$arg)
{
$this->db->update('mvc', $data, "`id` = $arg");
}
}
?>

Here is the view file index.php which inside views folder contains the update form
views/index/index.php

<?php
$name=$this->content[0]['name'];
$email=$this->content[0]['email'];
$contact=$this->content[0]['contact'];
?>

<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<form action="<?php echo URL; ?>index/edit_submit_index" method="post" enctype="multipart/form-data"onsubmit="return confirm('Do you really want to submit the form?');">
<div class="col-xs-6 form-group">
<label class="control-label col-xs-6" for="name">name:</label>
<div class="col-xs-6">
<input class="form-control" name="name" id="name" placeholder="Enter name" value="<?php echo $name; ?>">
</div>
</div>
<div class="col-xs-6 form-group">
<label class="control-label col-xs-6" for="email">email:</label>
<div class="col-xs-6">
<input type="text" class="form-control" name="email" id="email" placeholder="Enter email" value="<?phpecho $email; ?>">
</div>
</div>
<div class="col-xs-6 form-group">
<label class="control-label col-xs-6" for="contact">contact:</label>
<div class="col-xs-6">
<input type="text" class="form-control" name="contact" id="contact" placeholder="Enter contact"value="<?php echo $contact; ?>">
</div>
</div>
<div class="col-xs-6 form-group"> 
<div class="col-sm-offset-2 col-xs-6">
<button type="submit" class=".btn-info form-control" value="update" name="submit">Update</button>
</div>
</div>
</form>
</div>
</div>
</div>

Path: localhost/project_folder_name/view_folder_name/view_filename
Example: localhost/mvc/index/index