Hi..hello viewers!.. Welcome to "Parallel Code"
CRUD (Create Reterive Update Delete) operation in php tamil
CRUD Operations செய்வதற்கு தனி தனியான சில php file-கள் தேவைபடுகிறது. கீழே கொடுக்கப்பட்டுளவாறு file-களை உங்கள் project folder-க்கு உள்ள create செய்து கொள்ளுங்கள்.
File Creation
- config.php
- menu.php
- create.php
- retrive.php
- edit.php
- manfunctions.php
மேலே உள்ள file-களை போல நமக்கு சில தனி தனியான folder-களும் தேவைபடுகிறது. கீழே கொடுக்கப்பட்டுளவாறு folder-களை உங்கள் project folder-க்கு உள்ளே create செய்து கொள்ளுங்கள்.
Folder Creation
- css
- js
- profilepic
"css" folder, project-க்கு தேவையான அனைத்து style file-களையும் manage செய்ய உதவுகிறது. இதில் crudstyles.css என்ற file-ஐ create செய்து கொள்ளவேண்டும். இதற்கான css style கீழே கொடுக்கப்பட்டு உள்ளது அதை copy paste செய்து கொள்ளுங்கள்.
"js" folder project-க்கு தேவையான அனைத்து javascript file-களையும் வைத்துகொள்ள உதவுகிறது. இதில் myscript.js என்ற file-ஐ create செய்து கொள்ளவேண்டும். இதற்கான js script கீழே கொடுக்கப்பட்டு உள்ளது அதை copy paste செய்து கொள்ளுங்கள். jquery-3.6.0.min இந்த file-ஐ இந்த link கிளிக் செய்து download செய்து கொள்ளலாம். Click and download jquery-3.0.6.1.min.js
"profilepic" folder user-ன் profile picture-ஐ சேமித்து வைத்துகொள்ள உதவுகிறது.
Note: இந்த project-க்கு தேவையான முழு source code-ஐ இந்த Download Source Code button-ஐ கிளிக் செய்து download செய்து கொள்ளுங்கள். அல்லது கீழே தனி தனியாக கொடுக்கப்பட்டு உள்ள source code-ஐ copy paste செய்து கொள்ளுங்கள்.
1. confing.php
<?php
$link = mysqli_connect('localhost', 'root', '', 'test');
if (!$link) {
die('Connection error' . mysqli_connect_error());
}
?>
இந்த page database connection-ஐ உருவாக்க பயன்படுகிறது. இதில் உருவாக்கப்பட்ட database connection $link என்ற variable-லில் store செய்யபடுகிறது. நமக்கு எப்பொழுதெல்லாம் db connection தேவைபடுகிறதோ அப்பொழுது இந்த $link variable-ஐ தான் நாம் பயன்படுத்த வேண்டும். இந்த db connection-ஐ ஒவ்வொரு முறையும் புதிது புதிதாக create செய்து பயன்படுத்துவதற்கு பதிலாக ஒரு முறை உருவாக பட்ட $link variable-லையே பயன்படுத்தி கொள்ளலாம். இப்படி பயன்படுத்தவேண்டும் எனில் இந்த config.php page-ஐ include "config.php" என்று கொடுத்த பிறகு தான் $link variable-ஐ நாம் பயன்படுத்த முடியும்.
2. menu.php
<ul>
<li><a href="create.php">Create Employee</a></li>
<li><a href="retrive.php">Employee List</a></li>
</ul>
இந்த page நமக்கு தேவையான menu-களை உருவாக்க பயன்படுகிறது. இந்த menu page-ஐ include "menu.php" என்று கொடுத்து நமக்கு தேவையான page-களில் சேர்த்துகொள்ள வேண்டும்.
3. create.php
<?php
include 'config.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">
<form class="empform" id="empform" action="" 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" /></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="empname" id="empname" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="email" name="empemail" id="empemail" /></td>
</tr>
<tr>
<td>Photo</td>
<td><input type="file" name="profilepic" id="profilepic" /></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>
இந்த create.php page-ல் user-க்கு தேவையான அனைத்து field-களையும் html மற்றும் css பயன்படுத்தி design செய்யபட்டிருக்கிறது. DB-ல் எத்தனை data சேமிக்க வேண்டுமோ அத்தனை field-களை இங்கே உருவாக்க வேண்டும். இந்த field-கள் அனைத்தும் form tag-க்கு உள்ளே இருக்குமாறு அமைக்கவேண்டும்.
4. retrive.php
<?php
include 'config.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
$query = "SELECT * from emp";
$result = mysqli_query($link, $query);
if (mysqli_num_rows($result) > 0) {
?>
<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
while ($row = mysqli_fetch_assoc($result)) {
?>
<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="javascript:void(0)" onclick="deleteFunction('<?= $row['id'] ?>',this)">Delete</a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
} else {
echo 'Record Not found';
}
?>
</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 page-ல் DB-ல் உள்ள user-ன் அனைத்து data-வையும் எடுத்து ஒரு table வடிவில் நமக்கு காட்டுகிறது.
5. edit.php
<?php
include 'config.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
$query = "SELECT * from emp where id=$empid";
$result = mysqli_query($link, $query);
if (mysqli_num_rows($result) > 0) {
?>
<form class="empform" id="updateform" enctype="multipart/form-data">
<table cellspacing="20">
<tr>
<th colspan="2"><center>New Employee Form</center> <span id="update-status"></span></th>
</tr>
<?php
while ($row = mysqli_fetch_assoc($result)) {
?>
<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>
<script src="js/jquery-3.6.0.min.js" type="text/javascript"></script>
<script src="js/myscript.js" type="text/javascript"></script>
</body>
</html>
இந்த edit.php page-ல் எந்த user-ஐ நாம் edit செய்யவேண்டுமோ அந்த user-ன் data இங்கே form-ல் கொண்டு வந்து காட்டுகிறது. இதில் நமக்கு தேவையான மாற்றங்களை செய்த பிறகு update என்ற button-ஐ கிளிக் செய்து update செய்து கொள்ளலாம்.
6. manfunctions.php
<?php
include 'config.php';
extract($_REQUEST);
if (isset($submit) && $submit == 'create') {
$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');
echo 'success';
} else {
echo mysqli_error($link);
}
} else {
echo 'You have choosen ' . $ext . ' file. Please choose png or jpg or jpeg file.';
}
} else if (isset($submit) && $submit == 'update') {
if ($_FILES['profilepic']['size'] == 0) {
$query = "UPDATE emp set name='$empname',email='$empemail' where id='$empid'";
$result = mysqli_query($link, $query);
if ($result) {
echo 'success';
// 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');
echo 'success';
} else {
echo mysqli_error($link);
}
} else {
echo 'You have choosen ' . $ext . ' file. Please choose png or jpg or jpeg file.';
}
}
} else if (isset($submit) && $submit == 'delete') {
$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');
echo 'success';
}
}
7. crudstyles.css
ul li{
list-style-type: none;
margin-bottom: 20px;
}
.empform{
background-color: lightblue;
}
.sbtn{
padding: 5px 10px;
font-size: 20px;
background-color: #03a9f4;
color: #fff;
}
.container{
width: 80%;
margin: auto;
}
.left{
width: 30%;
float: left;
}
.right{
width: 70%;
float: left;
}
.emplist,.emplist td,.emplist th{
border:1px solid black;
border-collapse: collapse;
padding: 10px;
}
8. myscrip.js
$(document).ready(function () {
$('#empform').on('submit', function (e) {
e.preventDefault();
var id = $('#empid').val();
var name = $('#empname').val();
var email = $('#empemail').val();
var pic = $('#profilepic')[0].files;
var formData = new FormData($('#empform')[0]);
formData.append('submit', 'create');
if (id != '' && name != '' && email != '' && pic.length > 0) {
$.ajax({
type: 'post',
url: 'manfunctions.php',
data: formData,
cache: false,
contentType: false,
processData: false,
success: function (response) {
if (response == 'success') {
$('#empform').trigger('reset');
} else {
alert(response);
}
}
});
} else {
alert('Please provide all the information');
}
});
$('#updateform').on('submit', function (e) {
e.preventDefault();
var id = $('#empid').val();
var name = $('#empname').val();
var email = $('#empemail').val();
//var pic = $('#profilepic')[0].files;
if (id != '' && name != '' && email != '') {
var formData = new FormData($('#updateform')[0]);
formData.append('submit', 'update');
$.ajax({
type: 'post',
url: 'manfunctions.php',
data: formData,
cache: false,
contentType: false,
processData: false,
success: function (response) {
if (response == 'success') {
$('#update-status').text('Successfully Updated');
} else {
alert(response);
}
}
});
} else {
alert('Please provide all the information');
}
});
});
function deleteFunction(id,element) {
var res = confirm('Are you sure to delete this row?');
if (res) {
$.ajax({
type: 'post',
url: 'manfunctions.php',
cache: false,
data:{
empid: id,
submit:'delete'
},
success:function(response){
if(response=='success'){
alert('Successfully deleted');
$(element).closest('tr').remove();
}
}
});
}
}
இந்த manfunctions.php page-ல் create, update,delete போன்ற operation-களுக்கு தேவையான php code இதில் கொடுக்க பட்டுள்ளது.
Note: இந்த page-ல் உள்ள code உங்களுக்கு சரியாக புரியாத பட்சத்தில் இதற்க்கான முழு "Youtube" வீடியோ link இங்கு கொடுக்க பட்டுள்ளது. அதை கிளிக் செய்து நீங்கள் இன்னும் தெளிவாக புரிந்து கொள்ளலாம்.
Click and see the video
Comments