Site icon T4Tutorials.com

How To Delete The Record In PHP And MYSQL Database?

How To Delete The Record In PHP And MYSQL Database?

How To Delete The Record In PHP And MYSQL Database

In this tutorial, we will try to learn the about the deletion of records in the database table in PHP and MySQL.

Code of deletion of records in the database table in PHP and MySQL.

We can have two pages;

  1. index.php
  2. delete.php

Code of index.php by using MySQL and without MySQLi.

<?php
mysql_select_db('publisher',mysql_connect('localhost','root',''));
?>
<html><body>
<table border="1">
<?php
$books_query=mysql_query("select * from books");
while($books_rows=mysql_fetch_array($books_query)){
?>
<tr>
<td><?php echo $books_rows['BookID'] ; ?></td>
<td><?php echo $books_rows['BookTitleAttribute'] ; ?></td>
<td><a href="delete.php<?php echo '?id='.$books_rows['BookID']; ?>">delete</a></td>
</tr>
<?php }?>
</table> 
</body></html>

Code of delete.php by using MySQL and without MySQLi.

<?php
mysql_select_db('publisher',mysql_connect('localhost','root',''));
$id=$_GET['id'];
 mysql_query("delete from books where BookID='$id'");
header('location:index.php');
?>

Database

Figure: Database insert delete view update in PHP MySQL

Code of index.php by using MySQLi.

<?php
$conn = mysqli_connect('localhost','root','','publisher');
?>
<html><body>
<table border="1">
<?php
$books_query=mysqli_query($conn,"select * from books");
while($books_rows=mysqli_fetch_array($books_query)){
?>
<tr>
<td><?php echo $books_rows['BookID'] ; ?></td>
<td><?php echo $books_rows['BookTitleAttribute'] ; ?></td>
<td><a href="delete.php<?php echo '?id='.$books_rows['BookID']; ?>">delete</a></td>
</tr>
<?php }?>
</table> 
</body></html>

Code of delete.php by using MySQLi.

<?php
$conn = mysqli_connect('localhost','root','','publisher');
$id=$_GET['id'];
 mysqli_query($conn,"delete from books where BookID='$id'");
header('location:index.php');
?>
Exit mobile version