In this tutorial, We gonna learn How to delete the record in php mysqli
7. How to delete the data in a table
(We have two pages)
1. index.php (it is a interface)
2. process.php (it is a part of coding file)
Complete PHP Coding Reference Resource File
Step 1: (index.php)
<?php //Database Connection ?>
<?php $mysqli= new mysqli('localhost','root','','crud') or die(mysqli_error($mysqli)); ?>
<?php //includes files ?>
<?php require_once 'process.php'; ?>
<?php
$mysqli= new mysqli('localhost','root','','crud') or die(mysqli_error($mysqli));
$result= $mysqli->query("SELECT * FROM data") or die(mysqli_error($mysqli));
?>
<div class="row justify-content-center">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Location</th>
<th clospan="2">Action</th>
</tr>
<tbody>
<?php while ($row = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['location'];?></td>
<td>
<a href="index.php?edit=<?php echo $row['id']; ?>" class="btn btn-info">Edit</a>
<a href="process.php?delete=<?php echo $row['id']; ?>" class="btn btn-danger">Delete</a>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</thead>
</table>
</div>
<div class="row justify-content-center">
<form action="process.php" method="POST">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div class="form-group">
<label>Name:</label>
<input type="text" name="name" class="form-control" value="<?php echo $name; ?>" placeholder="Enter your name"/>
</div>
<div class="form-group">
<label>Location:</label>
<input type="text" name="location" class="form-control" value="<?php echo $location; ?>" placeholder="Enter your location"/>
</div>
<div class="form-group">
<?php if($update == true): ?>
<button class="btn btn-info" type="submit" name="update">Update</button>
<?php else: ?>
<button type="submit" class="btn btn-primary" name="save">Save</button>
<?php endif; ?>
</div>
</form>
</div>
Step 2: (process.php)
<?php
session_start();
$mysqli= new mysqli('localhost','root','','crud') or die(mysqli_error($mysqli));
if(isset($_GET['edit'])){
$id=$_GET['edit'];
$update=true;
$result = $mysqli->query("SELECT * FROM data WHERE id='$id'") or die(mysqli_error($mysqli));
if (count($result)==1){
$row= $result->fetch_array();
$name= $row['name'];
$location= $row['location'];
}
}
?>
No comments:
Post a Comment