OOPs Tutorials
- What is OOPs
- Access Modifier
- Constructor
- Destructor
- Polymorphism
- Inheritence
- Method Overriding
- Abstract class
- Interface
- Traits
- This keyword
- static keyword
- Final keyword
- Class Constant
- Exception Handling
- Binding in php
- tostring in php
- Object Iteration
- Reflection
- Magic methods
- Namespaces
- CRUD Function
- OOPs CRUD
- CRUD Example
Important Links
OOPs Crud project
Database Connection using class
File name : config.php
<?php
define("DB_HOST", 'localhost');
define("DB_USER", 'root');
define("DB_PASSWORD", '');
define("DB_DATABSE", 'oops');
?>
File name : dbConnect.php
<?php
class dbConnect {
/*function __construct() {
require_once('config.php');
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysqli_select_db($conn,DB_DATABSE);
if(!$conn)// testing the connection
{
die ("Cannot connect to the database");
}
return $conn;
}*/
// Another method by function
public function connect()
{
require_once('config.php');
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysqli_select_db($conn,DB_DATABSE);
if(!$conn)// testing the connection
{
die ("Cannot connect to the database");
}
return $conn;
}
public function Close(){
mysqli_close();
}
}
?>
File name : dbFunction.php
<?php
session_start();
require_once('config.php');
//require_once 'dbConnect.php';
class dbFunction {
public $conn;
function __construct() {
// connecting to database
//$db = new dbConnect();
$this->conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysqli_select_db($this->conn,DB_DATABSE);
if(!$this->conn)// testing the connection
{
die ("Cannot connect to the database");
}
return $this->conn;
}
// destructor
function __destruct() {
}
public function UserRegister($username, $emailid, $password){
$password = md5($password);
//$qr = mysql_query("INSERT INTO users(username, emailid, password) values('".$username."','".$emailid."','".$password."')") or die(mysql_error());
//return $qr;
$query = "INSERT INTO users(username, email, password) values('".$username."','".$emailid."','".$password."')";
$result = mysqli_query($this->conn,$query);
return $result;
}
public function Login($emailid, $password){
//$conn=$this->connect();
//$db = new dbConnect();
//$conn = $db->connect();
$res = mysqli_query($this->conn,"SELECT * FROM users WHERE email = '".$emailid."' AND password = '".md5($password)."'");
$user_data = mysqli_fetch_array($res);
$no_rows = mysqli_num_rows($res);
//$count_row = $res->num_rows;
if ($no_rows == 1)
{
$_SESSION['login'] = true;
$_SESSION['uid'] = $user_data['id'];
$_SESSION['username'] = $user_data['username'];
$_SESSION['email'] = $user_data['email'];
return TRUE;
}
else
{
return FALSE;
}
}
public function isUserExist($emailid){
$qr = mysqli_query($this->conn,"SELECT * FROM users WHERE email = '".$emailid."'");
echo $row = mysqli_num_rows($qr);
if($row > 0){
return true;
} else {
return false;
}
}
/*** for registration process ***/
public function reg_user($name,$username,$password,$email){
$password = md5($password);
$sql="SELECT * FROM users WHERE uname='$username' OR uemail='$email'";
//checking if the username or email is available in db
$check = $this->db->query($sql) ;
$count_row = $check->num_rows;
//if the username is not in db then insert to the table
if ($count_row == 0){
$sql1="INSERT INTO users SET uname='$username', upass='$password', fullname='$name', uemail='$email'";
$result = mysqli_query($this->db,$sql1) or die(mysqli_connect_errno()."Data cannot inserted");
return $result;
}
else { return false;}
}
/*** for showing the username or fullname ***/
public function get_fullname($uid){
$sql3="SELECT fullname FROM users WHERE uid = $uid";
$result = mysqli_query($this->db,$sql3);
$user_data = mysqli_fetch_array($result);
echo $user_data['fullname'];
}
/*** starting the session ***/
public function get_session(){
return $_SESSION['login'];
}
public function user_logout() {
$_SESSION['login'] = FALSE;
session_destroy();
}
}
?>
File name : index.php
File name : userregistration.php
<?php
include_once('dbFunction.php');
$funObj = new dbFunction();
if(isset($_POST['login'])){
$emailid = $_POST['emailid'];
$password = $_POST['password'];
// Escape user inputs for security
/*
$emailid = mysqli_real_escape_string($_REQUEST['emailid']);
$password = mysqli_real_escape_string($_REQUEST['password']);
*/
$user = $funObj->Login($emailid, $password);
if ($user) {
// Registration Success
header("location:home.php");
} else {
// Registration Failed
echo "<script>alert('Emailid / Password Not Match')</script>";
}
}
if(isset($_POST['register'])){
$username = $_POST['username'];
$emailid = $_POST['emailid'];
$password = $_POST['password'];
$confirmPassword = $_POST['confirm_password'];
if($password == $confirmPassword){
$email = $funObj->isUserExist($emailid);
if(!$email){
$register = $funObj->UserRegister($username, $emailid, $password);
if($register){
echo "<script>alert('Registration Successful')</script>";
}else{
echo "<script>alert('Registration Not Successful')</script>";
}
} else {
echo "<script>alert('Email Already Exist')</script>";
}
} else {
echo "<script>alert('Password Not Match')</script>";
}
}
?>
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="UTF-8" />
<title>Login and Registration Form with HTML5 and CSS3</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Login and Registration Form with HTML5 and CSS3" />
<meta name="keywords" content="html5, css3, form, switch, animation, :target, pseudo-class" />
<meta name="author" content="Codrops" />
<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" type="text/css" href="css/demo.css" />
<link rel="stylesheet" type="text/css" href="css/style2.css" />
<link rel="stylesheet" type="text/css" href="css/animate-custom.css" />
</head>
<body>
<div class="container">
<header>
<h1>Login and Registration Form </h1>
</header>
<section>
<div id="container_demo" >
<a class="hiddenanchor" id="toregister"></a>
<a class="hiddenanchor" id="tologin"></a>
<div id="wrapper">
<div id="login" class="animate form">
<form name="login" method="post" action="">
<h1>Log in</h1>
<p>
<label for="emailsignup" class="youmail" data-icon="e" > Your email</label>
<input id="emailsignup" name="emailid" required="required" type="email" placeholder="mysupermail@mail.com"/>
</p>
<p>
<label for="password" class="youpasswd" data-icon="p"> Your password </label>
<input id="password" name="password" required="required" type="password" placeholder="eg. X8df!90EO" />
</p>
<p class="keeplogin">
<input type="checkbox" name="loginkeeping" id="loginkeeping" value="loginkeeping" />
<label for="loginkeeping">Keep me logged in</label>
</p>
<p class="login button">
<input type="submit" name="login" value="Login" />
</p>
<p class="change_link">
Not a member yet ?
<a href="#toregister" class="to_register">Join us</a>
</p>
</form>
</div>
<div id="register" class="animate form">
<form name="login" method="post" action="">
<h1> Sign up </h1>
<p>
<label for="usernamesignup" class="uname" data-icon="u">Your username</label>
<input id="usernamesignup" name="username" required="required" type="text" placeholder="mysuperusername690" />
</p>
<p>
<label for="emailsignup" class="youmail" data-icon="e" > Your email</label>
<input id="emailsignup" name="emailid" required="required" type="email" placeholder="mysupermail@mail.com"/>
</p>
<p>
<label for="passwordsignup" class="youpasswd" data-icon="p">Your password </label>
<input id="passwordsignup" name="password" required="required" type="password" placeholder="eg. X8df!90EO"/>
</p>
<p>
<label for="passwordsignup_confirm" class="youpasswd" data-icon="p">Please confirm your password </label>
<input id="passwordsignup_confirm" name="confirm_password" required="required" type="password" placeholder="eg. X8df!90EO"/>
</p>
<p class="signin button">
<input type="submit" name="register" value="Sign up"/>
</p>
<p class="change_link">
Already a member ?
<a href="#tologin" class="to_register"> Go and log in </a>
</p>
</form>
</div>
</div>
</div>
</section>
</div>
</body>
</html>
File name : home.php
<?php
include_once('dbFunction.php');
if(isset($_POST['welcome'])){
// remove all session variables
session_unset();
// destroy the session
session_destroy();
}
if(!($_SESSION)){
header("Location:index.php");
}
?>
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="UTF-8" />
<title>Login and Registration Form with HTML5 and CSS3</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Login and Registration Form with HTML5 and CSS3" />
<meta name="keywords" content="html5, css3, form, switch, animation, :target, pseudo-class" />
<meta name="author" content="Codrops" />
<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" type="text/css" href="css/demo.css" />
<link rel="stylesheet" type="text/css" href="css/style2.css" />
<link rel="stylesheet" type="text/css" href="css/animate-custom.css" />
</head>
<body>
<div class="container">
<header>
<h1>Welcome Form </h1>
</header>
<section>
<div id="container_demo" >
<a class="hiddenanchor" id="toregister"></a>
<a class="hiddenanchor" id="tologin"></a>
<div id="wrapper">
<div id="login" class="animate form">
<form name="login" method="post" action="">
<h1>Welcome </h1>
<p>
<label for="emailid" class="uname" > Your Name </label>
<?=$_SESSION['username']?>
</p>
<p>
<label for="email" class="youpasswd" > Your Email </label>
<?=$_SESSION['email']?>
</p>
<p class="login button">
<input type="submit" name="welcome" value="Logout" />
</p>
</form>
</div>
</div>
</div>
</section>
</div>
</body>
</html>
Example 2 :-
File name : function.php
<?php
define('DB_SERVER','localhost');// Your hostname
define('DB_USER','root'); // Databse username
define('DB_PASS' ,''); // Database Password
define('DB_NAME', 'oops');// Database name
class DB_con
{
function __construct()
{
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
$this->dbh=$con;
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
}
// for username availblty
public function usernameavailblty($uname) {
$result =mysqli_query($this->dbh,"SELECT Username FROM tblusers WHERE Username='$uname'");
return $result;
}
// Function for registration
public function registration($uname,$uemail,$pasword)
{
$ret=mysqli_query($this->dbh,"insert into tblusers(Username,UserEmail,Password) values('$uname','$uemail','$pasword')");
return $ret;
}
// Function for signin
public function signin($uname,$pasword)
{
$result=mysqli_query($this->dbh,"select * from tblusers where Username='$uname' and Password='$pasword'");
//return $query;
$data = mysqli_fetch_array($result);
$count_row = mysqli_num_rows($result);
if ($count_row == 1) {
$_SESSION['login'] = true;
$_SESSION['username'] = $uname;
$_SESSION['id'] = $data['id'];
return true;
} else {
return false;
}
}
public function fetchdata()
{
$result=mysqli_query($this->dbh,"select * from users");
return $result;
}
}
?>
File name : fetchall.php
<?php
include_once("function.php");
$fetchdata=new DB_con();
?>
<table width="100%" border="0" >
<tr>
<th width="9%" height="42" scope="col" >S no.</th>
<th width="13%" scope="col">Name</th>
<th width="11%" scope="col">Email</th>
</tr>
<?php
$sql=$fetchdata->fetchdata();
$cnt=1;
while($row=mysqli_fetch_array($sql))
{
?>
<tr>
<td height="29"><?php echo $cnt;?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['email'];?></td>
</tr>
<?php $cnt=$cnt+1;} ?>
</table>
File name : index.php
File name : index.php
File name : index.php
<?php
session_start();
// include Function file
include_once('function.php');
// Object creation
$userdata=new DB_con();
if(isset($_POST['submit']))
{
// Posted Values
$uname=$_POST['user_name'];
$pasword=md5($_POST['user_password']);
//Function Calling
$sql=$userdata->signin($uname,$pasword);
//$_SESSION["fname"] = "mahtab";
if($sql)
{
// Message for successfull insertion
echo "<script>alert('Login successfull.');</script>";
echo "<script>window.location.href='dashboard.php'</script>";
}
else
{
// Message for unsuccessfull insertion
echo "<script>alert('Something went wrong. Please try again');</script>";
echo "<script>window.location.href='index.php'</script>";
}
}
?>
form class="form-horizontal m-t-20" action="" method="POST">
<div class="form-group row">
<div class="col-12">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="mdi mdi-account"></i></span>
</div>
<input class="form-control" type="text" name="user_name" id="user_name" placeholder="Username">
</div>
<span class="help-block" style="color:red;"></span>
</div>
</div>
<div class="form-group row">
<div class="col-12">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="mdi mdi-radar"></i></span>
</div>
<input class="form-control" type="password" name="user_password" id="user_password" placeholder="Password">
</div>
<span class="help-block" style="color:red;"></span>
</div>
</div>
<div class="form-group row">
<div class="col-12">
<div class="checkbox checkbox-primary">
<input id="checkbox-signup" type="checkbox">
<label for="checkbox-signup">
Remember me
</label>
</div>
</div>
</div>
<div class="form-group row">
<div class="col-12">
<a href="register.php">Register Here</a>
</div>
</div>
<div class="form-group text-right m-t-20">
<div class="col-xs-12">
<button class="btn btn-primary btn-custom w-md waves-effect waves-light" type="submit" name="submit" id="submit">Log In
</button>
</div>
</div>
<div class="form-group row m-t-30">
<div class="col-sm-7">
<a href="login/forgotpassword" class="text-muted"><i class="fa fa-lock m-r-5"></i> Forgot your
password?</a>
</div>
</div>
</form>
File name : index.php
File name : dashboard.php
<?php
session_start();
/*
if(!isset($_SESSION['username']))
{
header("Location: login.php");
}
*/
if(strlen($_SESSION['username'])=="")
{
header('location:logout.php');
}
?>
File name : register.php
<?php
// include Function file
include_once('function.php');
// Object creation
$userdata=new DB_con();
if(isset($_POST['submit']))
{
// Posted Values
$uname=$_POST['username'];
$uemail=$_POST['email'];
$pasword=md5($_POST['password']);
//Function Calling
$sql=$userdata->registration($uname,$uemail,$pasword);
if($sql)
{
// Message for successfull insertion
echo "<script>alert('Registration successfull.');</script>";
echo "<script>window.location.href='index.php'</script>";
}
else
{
// Message for unsuccessfull insertion
echo "<script>alert('Something went wrong. Please try again');</script>";
echo "<script>window.location.href='register.php'</script>";
}
}
?>
<form class="form-horizontal m-t-20" action="" method="post">
<div class="form-group row">
<div class="col-12">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="mdi mdi-email"></i></span>
</div>
<input class="form-control" type="email" name="email" id="email" required="" placeholder="Email">
</div>
</div>
</div>
<div class="form-group row">
<div class="col-12">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="mdi mdi-account"></i></span>
</div>
<input class="form-control" type="text" name="username" id="username" onblur="checkusername(this.value)" required="" placeholder="Username">
<span id="usernameavailblty"></span>
</div>
</div>
</div>
<div class="form-group row">
<div class="col-12">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="mdi mdi-key"></i></span>
</div>
<input class="form-control" type="password" name="password" id="password" required="" placeholder="Password">
</div>
</div>
</div>
<div class="form-group row">
<div class="col-12">
<div class="checkbox checkbox-primary">
<input id="checkbox-signup" type="checkbox" checked="checked">
<label for="checkbox-signup">
I accept <a href="#">Terms and Conditions</a>
</label>
</div>
</div>
</div>
<div class="form-group text-right m-t-20">
<div class="col-xs-12">
<button class="btn btn-primary btn-custom waves-effect waves-light w-md" type="submit" id="submit" name="submit">Register</button>
</div>
</div>
<div class="form-group row m-t-30">
<div class="col-12 text-center">
<a href="pages-login.html" class="text-muted">Already have account?</a>
</div>
</div>
</form>
<script src="assests/jquery-1.11.1.min.js"></script>
<script src="assests/bootstrap.min.js"></script>
<script>
function checkusername(va) {
$.ajax({
type: "POST",
url: "check_availability.php",
data:'username='+va,
success: function(data){
$("#usernameavailblty").html(data);
}
});
}
</script>
File name : logout.php
<?php
session_start();
session_destroy();
header('location:index.php');
?>
File name : delete.php
<td><a href="test_class.php?edit=1&post_id=<?php echo $post["post_id"]; ?>">Edit</a></td>
<td><a href="#" id="<?php echo $post["post_id"]; ?>" class="delete">Delete</a></td>
<script>
$(document).ready(function(){
$('.delete').click(function(){
var post_id = $(this).attr("id");
if(confirm("Are you sure you want to delete this post?"))
{
window.location = "test_class.php?delete=1&post_id="+post_id+"";
}
else
{
return false;
}
});
});
</script>
File name : index.php