+91-90427 10472
         
Dot net training in Chennai

CRUD operation in PHP with MySQL

Download and install XAMPP from the below url
https://www.apachefriends.org/download.html

Run the Xampp Exe File as below,
xampp-win32-7.0.13-0-VC14-installer.exe

Install XAMPP in the Customised location as below in E:\ drive.
Run Xampp_control.exe from XAMPP folder,

Start Apache and Mysql in XAMPP Control Panel

Click on Admin to redirect to MySQL Page,

Create employeedetails Database by clicking on New.

Click on Create to create database.
Click on New to create a new table.

Click on Save to create a new table.
Put the PHP files in htdocs folder as below,

Create CRUD folder in htdocs.

Create db.php,AddEmployee.php,index.php as shown below in CRUD folder.

Add below code in db.php,

<?php
$servername = “localhost”;
$username = “root”;
$password = “”;
$dbname = “employeedetails”;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
?>

Code in index.php,

<style type=”text/css”>
</style>
<?php
include(‘db.php’);
?>
<a href=”AddEmployee.php” value=”Create Employee” >Create</a>
<table cellspacing=”5″ cellpadding=”5″>
<tr>
<th>
Employee Id
</th>
<th>
Employee Name
</th>
<th>
Age
</th>
</tr>
<?php
$sql =”select * from employeedata”;
$result1 = $conn->query($sql);
if ($result1->num_rows > 0) {
// output data of each row
while($row = $result1->fetch_assoc())
{
?>
<tr>
<td>
<?php echo $row[“EmpId”]?>
</td>
<td>
<?php echo $row[“EmpName”]?>
</td>
<td>
<?php echo $row[“EmpAge”]?>
</td>
<td>
<a href=”AddEmployee.php?edited=1&id=<?php echo $row[“EmpId”]; ?>”>Edit</a>
</td>
<td>
<a href=””>Delete</a>
</td>
</tr>
<?php
}
}else {
echo 0;
}
?>
</table>

Code in AddEmployee.php as below,

<?php
include(‘db.php’);
$EmployeeID=”;
$EmployeeName=”;
$Age=”;
$Operation=’Create’;
if(isset($_POST[‘btnSave’]))
{
if($_POST[‘txtoperation’] ==”Create”)
{
$EmpId=$_POST[‘txtEmployeeID’];
$EmpName=$_POST[‘txtEmployeeName’];
$Age=$_POST[‘txtAge’];
$sql=”INSERT INTO Employeedata(EmpId, EmpName, EmpAge) VALUES (‘$EmpId’,’$EmpName’,’$Age’)”;
if ($conn->query($sql) === TRUE) {
echo “New record created successfully”;
header(‘Refresh:0; index.php’);
} else {
echo “Error: ” . $sql . “<br>” . $conn->error;
}
}
else
{
$EmpId=$_POST[‘txtEmployeeID’];
$EmpName=$_POST[‘txtEmployeeName’];
$Age=$_POST[‘txtAge’];
$sql=”UPDATE Employeedata SET EmpName=’$EmpName’,EmpAge=’$Age’ WHERE EmpId='{$_GET[‘id’]}'”;
if ($conn->query($sql) === TRUE) {
echo “New record updated successfully”;
header(‘Refresh:0; index.php’);
} else {
echo “Error: ” . $sql . “<br>” . $conn->error;
}
}
}
if(isset($_GET[‘edited’]))
{
$sql =”select * from employeedata where EmpId='{$_GET[‘id’]}'”;
$result1 = $conn->query($sql);
if ($result1->num_rows > 0) {
// output data of each row
if($row = $result1->fetch_assoc())
{
$EmployeeID=$row[“EmpId”];
$EmployeeName=$row[“EmpName”];
$Age=$row[“EmpAge”];
$Operation=”Edit”;
}
}
}
?>
<form action=”” method=”post”>
<table>
<tr>
<td>Employee Id</td>
<td><input type=”text” name=”txtEmployeeID” value=”<?php echo $EmployeeID; ?>” />
<input type=”hidden” name=”txtoperation” value=”<?php echo $Operation; ?>” />
</td>
</tr>
<tr>
<tr>
<td>Student Name</td>
<td><input type=”text” name=”txtEmployeeName” value=”<?php echo $EmployeeName; ?>” /></td>
</tr>
<tr>
<td>Age</td>
<td><input type=”text” name=”txtAge” value=”<?php echo $Age; ?>” /></td>
</tr>
<tr>
<td></td>
<td><input type=”submit” value=”save” name=”btnSave” /></td>
</tr>
</table>
</form>

The Output is as below,

http://localhost/CRUD/index.php

http://localhost/CRUD/AddEmployee.php


http://localhost/CRUD/AddEmployee.php?edited=1&id=2
Click below to download the solution,
https://1drv.ms/u/s!ArddhCoxftkQg6w5Te07v0wqAspR4g