First file that save in Name of insertdelupdate.php
<?php
include('server.php');
if(isset($_GET['edit']))
{
$id=$_GET['edit'];
$edit_state=true;
$rec = mysqli_query($db,"SELECT * FROM info where id=$id");
$record=mysqli_fetch_array($rec);
$name=$record['name'];
$address=$record['address'];
$id=$record['id'];
}
?>
<html>
<head>
<title>Insert, Update, Delete Demo</title>
<link rel="stylesheet" href="insertdelupadate.css">
</head>
<body>
<?php if(isset($_SESSION['msg'])): ?>
<div class="msg">
<?php
echo $_SESSION['msg'];
unset($_SESSION['msg']);
?>
</div>
<?php endif ?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
<?php while($row=mysqli_fetch_array($result))
{ ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><a href="insertdelupdate.php?edit=<?php echo $row['id']; ?> ">Edit</a></td>
<td><a href="server.php?del=<?php echo $row['id'];?>">Delete</a></td>
</tr>
<?php
}
?>
</tbody>
</table>
<form method="post" action="server.php">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div class="input-group">
<label>Name</label>
<input type="text" name="name" value="<?php echo $name; ?>">
</div>
<div class="input-group">
<label>Address</label>
<input type="text" name="address" value="<?php echo $address; ?>">
</div>
<div class="input-group">
<?php if($edit_state==false): ?>
<button type="submit" name="save" class="btn">Save</button>
<?php else: ?>
<button type="submit" name="Update" class="btn">Update</button>
<?php endif ?>
</div>
</form>
</body>
</html>
Second file save name of server.php
<?php
session_start();
$name="";
$address="";
$id=0;
$edit_state=false;
$db=mysqli_connect('localhost','root','','insertdelupdate');
if(isset($_POST['save']))
{
$name=$_POST['name'];
$address=$_POST['address'];
//insert recod
$query = "INSERT INTO info (name,address) VALUES ('$name','$address')";
mysqli_query($db, $query);
$_SESSION['msg']="Data is Inseted";
header('location:insertdelupdate.php');//redirect to page
}
//update Record
if(isset($_POST['Update']))
{
$name=mysqli_real_escape_string($db,$_POST['name']);
$address=mysqli_real_escape_string($db,$_POST['address']);
$id=mysqli_real_escape_string($db,$_POST['id']);
mysqli_query($db,"UPDATE info SET name='$name',address='$address' WHERE id=$id");
$_SESSION['msg']="Data is updated";
header('location:insertdelupdate.php');//redirect to page
}
//Delete Record
if(isset($_GET['del']))
{
$id=$_GET['del'];
mysqli_query($db,"DELETE FROM info where id=$id");
$_SESSION['msg']="Data is Deleted";
header('location:insertdelupdate.php');
}
//Retrive Record
$result = mysqli_query($db,"SELECT *FROM info");
//redirect to page
?>
css file
body
{
font-size:19px;
}
table{
width:50%;
margin:30px auto;
border-collapse:collapse;
text-align:left;
}
tr
{
border-bottom:1px solid #cbcbcb;
}
th,td
{
border:none;
height:20px;
padding:2px;
}
tr:hover
{
background:#F5F5F5;
}
form
{
width:45%;
margin:50px auto;
text-align:left;
padding:20px;
border:1px solid #bbbbbb;
border-radius:5px;
}
.msg
{
width:45%;
margin:50px auto;
text-align:left;
padding:20px;
border:1px solid #bbbbbb;
border-radius:5px;
}
<?php
include('server.php');
if(isset($_GET['edit']))
{
$id=$_GET['edit'];
$edit_state=true;
$rec = mysqli_query($db,"SELECT * FROM info where id=$id");
$record=mysqli_fetch_array($rec);
$name=$record['name'];
$address=$record['address'];
$id=$record['id'];
}
?>
<html>
<head>
<title>Insert, Update, Delete Demo</title>
<link rel="stylesheet" href="insertdelupadate.css">
</head>
<body>
<?php if(isset($_SESSION['msg'])): ?>
<div class="msg">
<?php
echo $_SESSION['msg'];
unset($_SESSION['msg']);
?>
</div>
<?php endif ?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
<?php while($row=mysqli_fetch_array($result))
{ ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><a href="insertdelupdate.php?edit=<?php echo $row['id']; ?> ">Edit</a></td>
<td><a href="server.php?del=<?php echo $row['id'];?>">Delete</a></td>
</tr>
<?php
}
?>
</tbody>
</table>
<form method="post" action="server.php">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div class="input-group">
<label>Name</label>
<input type="text" name="name" value="<?php echo $name; ?>">
</div>
<div class="input-group">
<label>Address</label>
<input type="text" name="address" value="<?php echo $address; ?>">
</div>
<div class="input-group">
<?php if($edit_state==false): ?>
<button type="submit" name="save" class="btn">Save</button>
<?php else: ?>
<button type="submit" name="Update" class="btn">Update</button>
<?php endif ?>
</div>
</form>
</body>
</html>
Second file save name of server.php
<?php
session_start();
$name="";
$address="";
$id=0;
$edit_state=false;
$db=mysqli_connect('localhost','root','','insertdelupdate');
if(isset($_POST['save']))
{
$name=$_POST['name'];
$address=$_POST['address'];
//insert recod
$query = "INSERT INTO info (name,address) VALUES ('$name','$address')";
mysqli_query($db, $query);
$_SESSION['msg']="Data is Inseted";
header('location:insertdelupdate.php');//redirect to page
}
//update Record
if(isset($_POST['Update']))
{
$name=mysqli_real_escape_string($db,$_POST['name']);
$address=mysqli_real_escape_string($db,$_POST['address']);
$id=mysqli_real_escape_string($db,$_POST['id']);
mysqli_query($db,"UPDATE info SET name='$name',address='$address' WHERE id=$id");
$_SESSION['msg']="Data is updated";
header('location:insertdelupdate.php');//redirect to page
}
//Delete Record
if(isset($_GET['del']))
{
$id=$_GET['del'];
mysqli_query($db,"DELETE FROM info where id=$id");
$_SESSION['msg']="Data is Deleted";
header('location:insertdelupdate.php');
}
//Retrive Record
$result = mysqli_query($db,"SELECT *FROM info");
//redirect to page
?>
css file
body
{
font-size:19px;
}
table{
width:50%;
margin:30px auto;
border-collapse:collapse;
text-align:left;
}
tr
{
border-bottom:1px solid #cbcbcb;
}
th,td
{
border:none;
height:20px;
padding:2px;
}
tr:hover
{
background:#F5F5F5;
}
form
{
width:45%;
margin:50px auto;
text-align:left;
padding:20px;
border:1px solid #bbbbbb;
border-radius:5px;
}
.msg
{
width:45%;
margin:50px auto;
text-align:left;
padding:20px;
border:1px solid #bbbbbb;
border-radius:5px;
}
Inset, Delete and Update in PHP |
1 comments:
fantastic post i learn a lot form this post keep it up .....
Replycheck
Insert Php Code
Post a Comment