1/12/19

PHP & MySQli Coding References - Part 3 | Delete Record


This is my part -3 php tutorial. before we have looked How to connect the database and view the data's as well as how to insert the data in php using mysqli

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)

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'; ?>

<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>
//These are Edit & Delete actions buttons
<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>

Step 2: (process.php)
<?php

$mysqli= new mysqli('localhost','root','','crud') or die(mysqli_error($mysqli));

if(isset($_GET['delete'])){
$id=$_GET['delete'];

$mysqli->query("DELETE FROM data WHERE id='$id'") or die(mysqli_error($mysqli));

}

?>

No comments:

Post a Comment

About

Hi, I'm Najathi.
I've started entrepreneurial company, Twin Brothers.Inc.
One is self-funded & the other is venture backed. I also send a weekly. where I share relevent, curated links.

Every Week I Publish a short post on writing, publishing, or content of IT Related

Contact Form

Name

Email *

Message *