PHP Delete Data From MySQL


PHP Delete Data From MySQL using MYSQLi Procedural

The DELETE statement is used to delete records from a table:


DELETE FROM table_name WHERE some_column = some_value




WHERE clause in the DELETE syntax:The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!

"fa_MyGuests" table:

The following example delete the record with id=2 in the "fa_MyGuests" table:

Example (MySQL Procedural)

<?php

include("mysql_connection.php");

$sql = "DELETE FROM fa_MyGuests WHERE id=2";

if (mysqli_query($conn,$sql)) {
echo "<script> alert('Record Deleted Successfully')</script>";
} else
{
echo "&l;script> alert('Recorde not Deleted.Please Try again.')</script>";
}

?>


mysqli_pop_connection.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db="lipsphpstudent";
// Create connection
$conn = mysql_connect($servername, $username, $password,$db);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_error());
}
?>
mysql_delete.php
<?php
include("mysqli_pop_connection.php");
if(isset($_GET['Delete'])) { $id=$_GET['Delete']; $sql = "DELETE FROM fa_MyGuests WHERE id=$id";
if (mysqli_query($conn,$sql)) {
echo "<script> alert('Record Deleted Successfully')</script>";
echo "<script> window.location.href='mysqli_pop_delete_live_example.php?';</script>";
} else { echo "<script> alert('Recorde not Deleted.Please Try again.')</script>";
echo "<script> window.location.href='mysqli_pop_delete_live_example.php';</script>";
} }
?>
mysqli_pop_delete_live_example.php
<?php
include("mysqli_pop_connection.php");
$sql="select * from fa_MyGuests ;
$query=mysqli_query($conn,$sql);
if($query)
{
?>
<?php include('header1.php'); ?>
<?php include("mysql_sidemenu.php"); ?>
<div class="container">
<div class="row">
<div class="col-sm-12">
<div class="table-responsive">

<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email ID</th>
<th>Reg_Date</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
while($row=mysqli_fetch_array($query))
{
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['firstname'];?></td>
<td><?php echo $row['lastname']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['reg_date']; ?></td>
<td><a href="mysql_delete.php?Delete=<?php echo $row['id']; ?>"><i class="fa fa-eraser"></i>Delete</a></td> </tr>
<?php
}
}?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php include('footer.php');?>
</div>
</div>