All In One Insert OOP
<?php
ini_set('mysql.connect_timeout',300);
ini_set('default_socket_timeout',300);
?>
<?php
include_once("all.php");
$checkdata=new Databases();
$error='';
$save='';
if(isset($_POST['submit'])){
$fname=$_POST['fname'];
$username=$_POST['username'];
$password=$_POST['password'];
$email=$_POST['email'];
$query=$checkdata->checkun($username);
if($query > 0 ) { //check if there is already an entry for that username
$error = "Username is already taken , Use another Username !!";
}else{
$filetmp = $_FILES["file_img"]["tmp_name"];
$filename = $_FILES["file_img"]["name"];
$filetype = $_FILES["file_img"]["type"];
$file_size =$_FILES['file_img']['size'];
$tmp_ext = explode( '.', $filename );
$file_ext = strtolower( end( $tmp_ext ) );
$filepath = "flimg/".$filename;
$expensions= array("jpeg","jpg","png","JPEG","JPG","PNG");
if(in_array($file_ext,$expensions)=== false){
$errors="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($filetmp,$filepath);
echo "Success";
}else{
print_r($errors);
}
$sql=$checkdata->imgreg($fname,$username,$password,$filename,$filepath,$filetype);
if (isset($_REQUEST['email'])) {
//Email information
$admin_email = "kistlakall@gmail.com";
$email = $_REQUEST['email'];
$subject = $_REQUEST['fname'];
$comment = $_REQUEST['username'];
//send email
mail($admin_email, "$subject", $comment, "From:" . $email);
}
$save = "".$_POST["fname"]." ".$_POST["username"]." , You Have Successfully Registed !! <br><br>
We will reply you as soon as possible to your email which you have filled in this form.<br><br>
Thank You !!<br><br>";
}
}
?>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<!-- Bootstrap core CSS -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
<style>
@media (max-width: 767px) {
.col-md-6 .col-sm-3 .hero-feature
{
visibility:hidden;
}
}
</style>
</head>
<body>
<div class="container"> <!-- Start Of The Container Class -->
<div class="row"> <!-- Start Of The Row Class -->
<div class="col-md-6 col-sm-12"> <!-- Start Of Right The Col Class -->
<br>
<form action="AllInOneInsert.php" method="post" enctype="multipart/form-data">
<h4><span><?php echo $error; ?></span></h4> <br>
<h4><span><?php echo $save; ?></span></h4> <br>
<div class="form-group">
<label>Name : *</label>
<input type="text" class="form-control" name="fname" placeholder="Enter Your Name" required>
</div>
<div class="form-group">
<label>Username : *</label>
<input type="text" class="form-control" name="username" placeholder="Enter Your Userame" required>
</div>
<div class="form-group">
<label>Password : *</label>
<input type="password" class="form-control" name="password" placeholder="Enter Your Password" required>
</div>
<div class="form-group">
<label>Email : *</label>
<input type="email" class="form-control" name="email" placeholder="Enter Your Password" required>
</div>
<div class="form-group">
<label>Upload Profile Picture :</label>
<input type="file" class="form-control-file" name="file_img" aria-describedby="fileHelp">
<small id="fileHelp" class="form-text text-muted">If U Want , U Can Skip Upload A Profile Picture</small>
</div>
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</form>
</div> <!-- End Of Right The Col Class -->
</div> <!-- End Of The Row Class -->
</div> <!-- End Of The Container Class -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"></script>
</body>
</html>
Delete OOP
<?php
include_once ('all.php');
$deletedata = new Databases;
if(isset($_GET['id']))
{
$id = $_GET['id'];
$up=$deletedata->del($id);
if($up)
{
echo "<meta http-equiv='refresh' content='0; url=select.php'>";
}
else
{
echo "Didnt Update";
}
}
?>
Update OOP
<?php
include_once ('all.php');
$updatedata = new Databases;
if(isset($_GET['id']))
{
$id = $_GET['id'];
if(isset($_POST['update']))
{
$name=$_POST['name'];
$username=$_POST['username'];
$password=$_POST['password'];
$up=$updatedata->update($id,$name,$username,$password);
if($up)
{
echo "<meta http-equiv='refresh' content='0; url=select.php'>";
}
else
{
echo "Didnt Update";
}
}
$vfurow=$updatedata->viewforup($id);
?>
<html>
<head>
</head>
<body>
<form action="" method="POST">
Name : <input type="text" name="name" value="<?php echo $vfurow['name'] ?>"> <br><br>
Username : <input type="text" name="username" value="<?php echo $vfurow['username'] ?>"> <br><br>
Password : <input type="text" name="password" value="<?php echo $vfurow['pw'] ?>"> <br><br>
<input type="submit" name="update" value="Update"> <br><br>
</form>
<?php
}
?>
</body>
</html>
View / Select OOP
<?php
include('Session.php');
include_once ('all.php');
$data = new Databases;
?>
<!DOCTYPE html>
<html>
<head>
<title>Webslesson Tutorial | Select or Fetch Data from Mysql Table using OOPS in PHP</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<br /><br />
<div class="container" style="width:700px;">
<br />
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<td width="30%">ID</td>
<td width="50">Name</td>
<td width="10%">Username</td>
<td width="10%">Password</td>
</tr>
<?php
$post_data = $data->select('academic');
foreach($post_data as $row)
{
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo substr($row["name"], 0, 200); ?></td>
<td><?php echo substr($row["username"], 0, 200); ?></td>
<td><?php echo substr($row["pw"], 0, 200); ?></td>
<td><a href="update.php?id=<?php echo $row["id"]; ?>">Edit</a></td>
<td><a href="deloop.php?id=<?php echo $row["id"]; ?>">Delete</a></td>
</tr>
<?php
}
?>
</table>
</div>
<h3><a href="Logout.php"> Logout </a></h3>
</div>
</body>
</html>
Insert With Folder Img
<?php
include_once("db.php");
$insertdata=new Databases();
if(isset($_POST['insert']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$num = $_POST['num'];
$msg = $_POST['msg'];
$target_dir = "flimg/";
$target_file = $target_dir . basename($_FILES["fileName"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$check = getimagesize($_FILES["fileName"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileName"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" && $imageFileType != "JPG" && $imageFileType != "PNG" && $imageFileType != "JPRG") {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileName"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileName"]["name"]). " has been uploaded.";
$sql=$insertdata->insert($name,$email,$num,$msg);
if (isset($_REQUEST['email'])) {
//Email information
$admin_email = "kistlakall@gmail.com";
$email = $_REQUEST['email'];
$subject = $_REQUEST['name'];
$comment = $_REQUEST['msg'];
//send email
mail($admin_email, "$subject", $comment, "From:" . $email);
}
} else {
echo "Sorry, there was an error uploading your Data and File.";
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title> PHP INSERT DATA </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
Name <input type="text" name="name"><br><br>
Email <input type="email" name="email"><br><br>
Number <input type="text" name="num"><br><br>
Message <input type="text" name="msg"><br><br>
Upload <input type="file" name="fileName"><br><br>
<input type="submit" name="insert" value="Add Data To Database">
</form>
</body>
</html>
Insert OOP
<?php
include_once ('all.php');
$data = new Databases;
$success_message = '';
if(isset($_POST["submit"]))
{
$insert_data = array(
'name' => mysqli_real_escape_string($data->con, $_POST['name']),
'username' => mysqli_real_escape_string($data->con, $_POST['username']),
'pw' => mysqli_real_escape_string($data->con, $_POST['password'])
);
if($data->insert('academic', $insert_data))
{
$success_message = 'Post Inserted';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Insert data into Table using OOPS in PHP</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<br /><br />
<div class="container" style="width:700px;">
<form method="post">
<label>Name</label>
<input type="text" name="name" class="form-control" />
<br />
<label>Username</label>
<input type="text" name="username" class="form-control" />
<br />
<label>Password</label>
<input type="password" name="password" class="form-control" />
<br />
<input type="submit" name="submit" class="btn btn-info" value="Submit" />
<span class="text-success">
<?php
if(isset($success_message))
{
echo $success_message;
}
?>
</span>
</form>
</div>
</body>
</html>
Session OOP
<?php
include_once ('all.php');
$sesdata = new Databases;
session_start();
$user_check=$_SESSION['login_user'];
$row=$sesdata->ses($user_check);
$login_session=$row['username'];
if(!isset($_SESSION['login_user']))
{
header('location: Login.php');
exit;
}
?>
Login Web OOP
<?php
include_once ('all.php');
$logindata = new Databases;
session_start();
$error='';
if(isset($_POST['login']))
{
if(empty($_POST['username']) || empty($_POST['password']))
{
$error = "Username Or Password Is Empty";
}
else
{
$username=$_POST['username'];
$password=$_POST['password'];
$rows=$logindata->login($username,$password);
if($rows==1)
{
$_SESSION['login_user']=$username;
header('location:select.php');
}
else
{
$error="Invalid Username Or Password";
}
}
}
?>
Login OOP
<?php
include('LoginWeb.php');
?>
<html>
<head>
</head>
<body>
<center>
<form action="" method="POST">
Username : <input type="text" name="username"> <br><br>
Password : <input type="password" name="password"> <br><br>
<input type="submit" name="login" value="Login"> <br><br>
<?php echo $error; ?>
</form>
</center>
</body>
</html>
OOP DB
<?php
class Databases{
public $con;
public function __construct()
{
$this->con = mysqli_connect("localhost", "root", "", "portal");
if(!$this->con)
{
echo 'Database Connection Error ' . mysqli_connect_error($this->con);
}
}
public function insert($table_name, $data)
{
$string = "INSERT INTO ".$table_name." (";
$string .= implode(",", array_keys($data)) . ') VALUES (';
$string .= "'" . implode("','", array_values($data)) . "')";
if(mysqli_query($this->con, $string))
{
return true;
}
else
{
echo mysqli_error($this->con);
}
}
public function insertsingle($name,$username,$password)
{
$string = "INSERT INTO academic (name,username,pw) VALUES ('$name','$username','$password')";
$rsint=mysqli_query($this->con, $string);
return $rsint;
}
public function select($table_name)
{
$array = array();
$query = "SELECT * FROM ".$table_name."";
$result = mysqli_query($this->con, $query);
while($row = mysqli_fetch_assoc($result))
{
$array[] = $row;
}
return $array;
}
public function viewforup($id)
{
$vfu="SELECT * FROM academic WHERE id='$id' ";
$rs = mysqli_query($this->con, $vfu);
$vfurow=mysqli_fetch_array($rs);
return $vfurow;
}
public function update($id,$name,$username,$password)
{
$up="UPDATE academic SET name='$name' , username='$username' , pw='$password' WHERE id='$id' ";
$uprs = mysqli_query($this->con, $up);
return $uprs;
}
public function del($id)
{
$deldel="DELETE FROM academic WHERE id=$id";
$delrs = mysqli_query($this->con, $deldel);
return $delrs;
}
public function login($username,$password)
{
$loginquery="SELECT * FROM academic WHERE username='$username' AND pw='$password' ";
$loginrs = mysqli_query($this->con, $loginquery);
$rows=mysqli_num_rows($loginrs);
return $rows;
}
public function ses($user_check)
{
$sesrs="SELECT username FROM academic WHERE username='$user_check' ";
$sesquery = mysqli_query($this->con, $sesrs);
$rowrs=mysqli_fetch_assoc($sesquery);
return $rowrs;
}
public function checkun($username)
{
$checkquery="SELECT * FROM academic WHERE username='$username' ";
$loginrs = mysqli_query($this->con, $checkquery);
$rowc=mysqli_num_rows($loginrs);
return $rowc;
}
public function imgreg($fname,$username,$password,$filename,$filepath,$filetype)
{
$imginsert = "INSERT INTO academic (fname,username,pw,img_name,img_path,img_type) VALUES ('$fname','$username','$password','$filename','$filepath','$filetype')";
$imgquery=mysqli_query($this->con, $imginsert);
return $imgquery;
}
}
?>
Img2
<?php
ini_set('mysql.connect_timeout',300);
ini_set('default_socket_timeout',300);
include('AdminSession.php');
?>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="img/WisdomIcon.jpg">
<!--JavaScript BootstrapFiles-->
<script src="js\bootstrap.js"></script>
<script src="js\bootstrap,mini.js"></script>
<script src="js\jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- Bootstrap CSS -->
<link href="css\bootstrap.css" rel="stylesheet">
<link href="css\bootstrap.min.css" rel="stylesheet">
<link href="css\bootstrap-theme.css" rel="stylesheet">
<link href="css\bootstrap-theme.min.css" rel="stylesheet">
<!-- Bootstrap core CSS -->
<!-- <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"> -->
<title>
<?php
include("title.php")
?>
</title>
<style>
h1, h3, h4
{
font-family: Times New Roman;
line-height: 180%;
text-align: left;
color:#353c47;
}
.thumbnail
{
height:300px;
width:350px;
text-align:center;
}
</style>
<?php
include("backtop.php")
?>
</head>
<?php
include("nav2logout.php")
?>
<?php
include("AdminNav.php")
?>
<body>
<div class="container"> <!-- Start Of The Container Class -->
<div class="row"> <!-- Start Of The Row Class -->
<div class="col-md-9 col-sm-4 hero-feature"> <!-- Start Of The Col Class -->
<h1> Customize Images Of The Site.</h1>
<form method="post" enctype="multipart/form-data">
<br>
Upload :<input type="file" name="image"/> <br>
<input type="submit" name="submit" value="Upload"/>
</form>
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
if(isset($_POST['submit']))
{
if(getimagesize($_FILES['image']['tmp_name'])== FALSE)
{
echo "Plz slt";
}
else
{
$image=addslashes($_FILES['image']['tmp_name']);
$name= addslashes($_FILES['image']['name']);
$image= file_get_contents($image);
$image= base64_encode($image);
saveimage($name,$image);
}
}
function saveimage($name,$image)
{
$con=mysql_connect("localhost","root","");
mysql_select_db("a3075631_slsoc",$con);
$qry="insert into image (name,image) values ('$name','$image')";
$result=mysql_query($qry,$con);
if($result)
{
echo "<meta http-equiv='refresh' content='0;url=img2.php'>";
}
else
{
echo "<br><h1>Not Uploaded</h1>";
}
}
include('ksdb.php');
// get results from database
$result = mysql_query("SELECT * FROM image")
or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo '
<div class="col-md-4 col-sm-6 hero-feature">
<div class="thumbnail">
<img height="300" width="300" src="data:image;base64,'.$row['image'].'"> <br>
ID - '.$row['id'].' <br>
Name - '.$row['name'].' <br>
<a href="img2Update.php?id=' . $row['id'] . '">Update</a>
<a href="img2Del.php?id=' . $row['id'] . '">Delete</a>
</div>
</div>
';
}
?>
</div> <!-- End Of The Col Class -->
</div> <!-- End Of The Row Class -->
</div> <!-- End Of The Container Class -->
<!-- Footer -->
<?php
include("footer.php")
?><!-- End Of The Footer -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"></script>
</body>
</html>
Folder Img
<?php
$dir_path = "flimg/";
$extensions_array = array('jpg','png','jpeg');
if(is_dir($dir_path))
{
$files = scandir($dir_path);
for($i = 0; $i < count($files); $i++)
{
if($files[$i] !='.' && $files[$i] !='..')
{
// get file name
echo "File Name -> $files[$i]<br>";
// get file extension
$file = pathinfo($files[$i]);
$extension = $file['extension'];
echo "File Extension-> $extension<br>";
// check file extension
if(in_array($extension, $extensions_array))
{
// show image
echo "<img src='$dir_path$files[$i]' style='width:100px;height:100px;'><br>";
}
}
}
}
?>
DB Con
<?php
$host = 'localhost';
$user = 'root';
$pw = '';
$db = 'a3075631_slsoc';
$connection = mysql_connect($host,$user,$pw)
or die ("Couldnt Connect" .mysql_error());
mysql_select_db($db)
or die ("Couldnt Find DB" .mysql_error());
?>
3 comments: