Hi..hello viewers!.. Welcome to "Parallel Code"
How to insert, fetch, update, delete records by OOP concept
database connection செய்வதற்கு DB என்ற class-ஐ create செய்து அதில் connection என்ற function-ல் connection-க்கு தேவையான அனைத்து source code-ஐயும் இதில் எழுதிவிடவேண்டும். இதில் connection-ஐ $con என்ற variable return செய்யவேண்டும். இந்த variable தான் நமக்கு connection-ஐ தருகிறது.
நமக்கு தேவையான அனைத்து coding-ம் இந்த ManFunctions என்ற class-ஐ உருவாக்கி இதில் எழுதிவிடவேண்டும். இதில் உள்ள ஒவ்வொரு function-ஐயும் நாம் ஒரு object-ன் உதவியோடுதான் call செய்யமுடியும். ஆகையால் $mf=new ManFunctions(); என்று ஒரு object-ஐ உருவாக்கி கொள்ளவேண்டும்.
Note: இந்த project-க்கு தேவையான முழு source code-ஐ இந்த Download Source Code button-ஐ கிளிக் செய்து download செய்து கொள்ளுங்கள். அல்லது கீழே தனி தனியாக கொடுக்கப்பட்டு உள்ள source code-ஐ copy paste செய்து கொள்ளுங்கள்.
config.php
<?php
class DB {
function connection() {
$con = mysqli_connect('localhost', 'root', '', 'test');
if (!$con) {
die('Connection error' . mysqli_connect_error());
}
return $con;
}
}
menu.php
<ul>
<li><a href="create.php">Create Employee</a></li>
<li><a href="retrive.php">Employee List</a></li>
</ul>
create.php
<!doctype html>
<html>
<head>
<title>Employee Form</title>
<link href="css/crudstyles.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="container">
<div class="left">
<?php require 'menu.php'; ?>
</div>
<div class="right">
<form class="empform" id="empform" method="post" action="manfunctions.php" enctype="multipart/form-data">
<table cellspacing="20">
<tr>
<th colspan="2"><center>New Employee Form</center></th>
</tr>
<tr>
<td>Emp ID</td>
<td><input type="text" name="empid" id="empid" required="" /></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="empname" id="empname" required=""/></td>
</tr>
<tr>
<td>Email</td>
<td><input type="email" name="empemail" id="empemail" required=""/></td>
</tr>
<tr>
<td>Photo</td>
<td><input type="file" name="profilepic" id="profilepic" required="" /></td>
</tr>
<tr>
<td></td>
<td><button class="sbtn" name="submit" id="submit" value="create">Submit</button></td>
</tr>
</table>
</form>
</div>
</div>
<!-- <script src="js/jquery-3.6.0.min.js" type="text/javascript"></script>
<script src="js/myscript.js" type="text/javascript"></script>-->
</body>
</html>
retrive.php
<?php
include 'manfunctions.php';
?>
<!doctype html>
<html>
<head>
<title>Employee list</title>
<link href="css/crudstyles.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="container">
<div class="left">
<?php require 'menu.php'; ?>
</div>
<div class="right">
<?php
?>
<h3>Employee List</h3>
<table class="emplist">
<thead>
<tr>
<th>Emp id</th>
<th>Name</th>
<th>Email</th>
<th>Profile</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$rows= $mf->getalldata($link,'emp');
if(!empty($rows)){
foreach($rows as $row){
?>
<tr>
<td><?= $row['id'] ?></td>
<td><?= $row['name'] ?></td>
<td><?= $row['email'] ?></td>
<td><img src="<?= $row['profilepic'] ?>" width="100" height="100"></td>
<td>
<a href="edit.php?empid=<?= $row['id'] ?>">Edit</a> |
<a href="manfunctions.php?empid=<?= $row['id'] ?>&submit=delete" onclick="return confirm('Are you sure to delete?')">Delete</a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
} else {
echo 'Record Not found';
}
?>
</div>
</div>
</body>
</html>
edit.php
<?php
include 'manfunctions.php';
extract($_REQUEST);
?>
<!doctype html>
<html>
<head>
<title>Employee list</title>
<link href="css/crudstyles.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="container">
<div class="left">
<?php require 'menu.php'; ?>
</div>
<div class="right">
<?php
$rows=$mf->getPaticularEmployee($link, 'emp', $empid);
if(!empty($rows)){
?>
<form class="empform" id="updateform" method="post" action="manfunctions.php" enctype="multipart/form-data">
<table cellspacing="20">
<tr>
<th colspan="2"><center>New Employee Form</center> <span id="update-status"></span></th>
</tr>
<?php
foreach($rows as $row) {
?>
<tr>
<td>Emp ID</td>
<td><input type="text" name="empid" id="empid" value="<?= $row['id'] ?>" readonly="" /></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="empname" id="empname" value="<?= $row['name'] ?>"/></td>
</tr>
<tr>
<td>Email</td>
<td><input type="email" name="empemail" id="empemail" value="<?= $row['email'] ?>"/></td>
</tr>
<tr>
<td>Photo</td>
<td><img src="<?= $row['profilepic'] ?>" alt="Profile Picture" width="200" height="200"/></td>
</tr>
<tr>
<td>Change(optional)</td>
<td><input type="file" name="profilepic" id="profilepic" /></td>
</tr>
<tr>
<td></td>
<td><button class="sbtn" name="submit" id="submit" value="update">Update</button></td>
</tr>
<?php
}
?>
</table>
</form>
<?php
} else {
echo 'Record Not found';
}
?>
</div>
</div>
</body>
</html>
manfunctions.php
இந்த page-ல் DB class-ஐ ManFunctions உடன் inherit செய்யவேண்டும் இதற்காக extends என்ற keyword-ஐ பயன்படுத்தவேண்டும். இவ்வாறு inheritance செய்வதன் மூலம் $mf என்ற object-ஐ கொண்டே இரண்டு class-களின் function-களையும் நாம் call செய்யமுடியும். இதில் $_REQUEST என்ற auto global variable-ஐ நமக்கு தேவையான இடங்களில் extract() செய்து கொள்கிறோம். இதில் $link என்ற variable-ஐ set செய்து பிறகு அதை extract($_REQUEST) என்று கொடுத்த பிறகு அந்த db connection $link என்ற variable-ஐ நாம் எடுத்து கொள்ளலாம்.
<?php
include 'config.php';
class ManFunctions extends DB {
function getalldata($link, $table) {
$query = "SELECT * from $table";
$result = mysqli_query($link, $query);
$res = [];
if (mysqli_num_rows($result) > 0) {
$res = mysqli_fetch_all($result, MYSQLI_ASSOC);
}
return $res;
}
function getPaticularEmployee($link, $table, $empid) {
$query = "SELECT * from $table where id=$empid";
$result = mysqli_query($link, $query);
$res = [];
if (mysqli_num_rows($result) > 0) {
$res = mysqli_fetch_all($result, MYSQLI_ASSOC);
}
return $res;
}
function create() {
extract($_REQUEST);
$fname = $_FILES['profilepic']['name'];
$tr = explode('.', $fname);
$ext = strtolower(end($tr));
if ($ext == 'jpg' || $ext == 'png' || $ext == 'jpeg') {
$dir = 'profilepic';
$newfname = $empid . '.' . $ext;
$profilepic_path = $dir . '/' . $newfname;
move_uploaded_file($_FILES['profilepic']['tmp_name'], $profilepic_path);
$query = "INSERT INTO emp(id,name,email,profilepic) values('$empid','$empname','$empemail','$profilepic_path')";
$result = mysqli_query($link, $query);
if ($result) {
header('location:create.php');
} else {
echo mysqli_error($link);
}
} else {
echo 'You have choosen ' . $ext . ' file. Please choose png or jpg or jpeg file.';
}
}
function update() {
extract($_REQUEST);
if ($_FILES['profilepic']['size'] == 0) {
$query = "UPDATE emp set name='$empname',email='$empemail' where id='$empid'";
$result = mysqli_query($link, $query);
if ($result) {
header('location:retrive.php');
} else {
echo mysqli_error($link);
}
} else {
$fname = $_FILES['profilepic']['name'];
$dd = explode('.', $fname);
$ext = strtolower(end($dd));
if ($ext == 'jpg' || $ext == 'png' || $ext == 'jpeg') {
$dir = 'profilepic';
$newfname = $empid . '.' . $ext;
$profilepic_path = $dir . '/' . $newfname;
move_uploaded_file($_FILES['profilepic']['tmp_name'], $profilepic_path);
$query = "UPDATE emp set name='$empname',email='$empemail',profilepic='$profilepic_path' where id='$empid'";
$result = mysqli_query($link, $query);
if ($result) {
header('location:retrive.php');
} else {
echo mysqli_error($link);
}
} else {
echo 'You have choosen ' . $ext . ' file. Please choose png or jpg or jpeg file.';
}
}
}
function delete() {
extract($_REQUEST);
$query = "SELECT profilepic from emp where id=$empid";
$result = mysqli_query($link, $query);
$row = mysqli_fetch_assoc($result);
$profilepic_path = $row['profilepic'];
if (file_exists($profilepic_path)) {
unlink($profilepic_path);
}
$dquery = "delete from emp where id=$empid";
$result = mysqli_query($link, $dquery);
if ($result) {
header('location:retrive.php');
}
}
}
$mf = new ManFunctions();
extract($_REQUEST);
$link=$mf->connection();
$_REQUEST['link']=$link;
if (isset($submit) && $submit == 'create') {
$mf->create();
} else if (isset($submit) && $submit == 'update') {
$mf->update();
} else if (isset($submit) && $submit == 'delete') {
$mf->delete();
}
Comments