Thursday, 6 June 2013

PDO And php tutorial



PDO AND PHP  TUTORIAL


This tutorial is helping that how to work with object oriented php using PDO.This tutorial contain easy and simple code and easily understood by all users.


Create  Database: `pdo`


-- Table structure for table `mail`
--
CREATE TABLE IF NOT EXISTS `mail` (
  `fname` varchar(50) NOT NULL,
  `lname` varchar(50) NOT NULL,
  `id` varchar(50) NOT NULL,
  `pwd` varchar(50) NOT NULL,
  `date` varchar(30) NOT NULL,
  `age` varchar(30) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


Create  php file for database connection : `config.php`


<?php
class connection
{
public function db_connect()
{
return new PDO("mysql:host=localhost; dbname=object","root","");
}
}
?>



Create  php file : `function.php`


<?php

include_once('config.php');

class user

{

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//For generate connection


private $db;

public function __construct()

{

$this->db = new connection();

$this->db = $this->db->db_connect();

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////ForLogin////////////////////////////////////////////////////////


public function login($id,$pwd)

{

if(!empty($id) && !empty($pwd))

{

$st = $this->db->prepare("select * from reg where id='$id' and pwd='$pwd'");

$st->bindParam(1,$id);

$st->bindParam(2,$pwd);

$st->execute();

if($st->rowCount()==1)

{

echo "user varyfied. Access granted";

else

{

echo "plz fill valid id and password";

}

}

else

{

echo "plz fill all fields";

}

}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////For Registration ////////////////////////////////////////////////////////////////////////////////////////


public function reg($fname,$lname,$id,$pwd,$cpwd,$date,$age)

{

$st = $this->db->prepare("select * from mail where id='$id'");

$st->bindParam(1,$id);

$st->execute();

if($st->rowCount()==1)

{

echo "user is already exists";

}

else

{

if($pwd==$cpwd)

{

$st = $this->db->prepare("insert into mail values('$fname','$lname','$id','$pwd','$date','$age')");

$st->execute();

echo "reg is completed";

}

else

{

echo "plz fill confirm pwd";

}

}

}

}

?>


Create  php file for login: `login.php`


<?php

include_once('function.php');

if(isset($_POST['login']))

{

$id=$_REQUEST['id'];

$pwd=$_REQUEST['pwd'];

$object = new user();

$object->login($id,$pwd);

}

?>


<form action="" method="post">

User id: <input type="text" name="id"/>

User password: <input type="password" name="pwd">

<input type="submit" name="login" value="Login"><a href="reg.php">New Registration</a>

</form>



Create  php file for registration: `reg.php`


<?php

include_once('function.php');

if(isset($_REQUEST['reg']))

{

$lname=$_REQUEST['lname'];

$fname=$_REQUEST['fname'];

$id=$_REQUEST['id'];

$pwd=$_REQUEST['pwd'];

$cpwd=$_REQUEST['cpwd'];

$date=$_REQUEST['date'];

$age=$_REQUEST['age'];

$object = new user();

$object->reg($fname,$lname,$id,$pwd,$cpwd,$date,$age);

}

?>


<form action="" method="post">

<p align="center">First Name: <input type="text" name="fname"><p>

<p align="center">Last Name: <input type="text" name="lname"><p>

<p align="center">User Id: <input type="email" name="id"><p>

<p align="center"> <input type="password" name="pwd"><p>

<p align="center">Re-enter Password: <input type="password" name="cpwd"><p>

<p align="center">&nbsp;&nbsp;Date: <input type="date" name="date"><p>

<p align="center">&nbsp;&nbsp;Age: <input type="number" max="100" min="18" name="age">Years<p>

<p align="center"><input type="submit" name="reg" value="register now"><p>

</form>

No comments:

Post a Comment