PHP Delete Data from Database


Prev Tutorial Next Tutorial

Delete Data from Database in PHP

To delete any record from database on webpage first you need to create user interface so here i am going to create Html page and search box. First search record from database if it present then record will delete. To delete any record from database we use below MYSQL query.

Syntax

delete from table_name where id=2;

connect-db.php

Connect to database

<?php

$server = 'localhost';
$user = 'user_name';
$pass = 'password';
$db = 'database_name';

// Connect to Database

$connection = mysql_connect($server, $user, $pass)
or die("Could not connect to server ... \n" . mysql_error ());

// select database
mysql_select_db($db)
or die("Could not connect to database ... \n" . mysql_error ());

?>

delete.html

Delete page

<?php
include 'db-connect.php';

mysql_select_db("tutorohx_rw", $conncection);

$valueToSearch = $_POST['valueToSearch'];

$sql = "SELECT * FROM letter_dispatch WHERE s_no=$valueToSearch";
$result = mysql_query($sql, $connection);

?>

<?php

if ($result)
 {
  while($row = mysql_fetch_array($result))
 {
?>

// Show Record

<h3>You Want to Delere Record:</h3>
<table class="table" border='0'>
<form method="post" action='delete-record-successfully.php' id="deleteform">
<tr><th>Serial no</th><td><span style="color:red; font-weight: bold; font-size:20px;"><?php echo $valueToSearch;?></span></td></tr>
<tr><th></th><td><input type=hidden name=s_no value=<?php echo $row['s_no']; ?>></td></tr>
<tr><td><input type="submit" form="deleteform" value="Delete"></td></tr>
</form>

<?php
 }
} 
?>
</table>

delete-record-successfully.php

Record Deleted

<?php

include 'db-connect.php';

mysql_select_db("tutorohx_rw", $conncection);

$s_no = $_POST['s_no'];

$sql = "DELETE FROM letter_dispatch where s_no='$s_no'";

$result = mysql_query($sql, $connection);
if($result)
{
 echo"Record Deleted Successfully........";
}
else
{
 echo"Try Again........";
}

?>

Prev Tutorial Next Tutorial

Google Advertisements
Download Projects

Google Adsense Advertisements

Buy 3 Year Old Domain, Tutorialday.com, SeosmoTools.com @$200

Buy This Ad Space @$50 per Month, Ad Size 300X250 Contact on: hitesh.xc@gmail.com WhatsApp no. 8882538826

Yahoo Advertisements