Example : How to Delete/Remove Records & Image from MySQL Database using PHP?
<?php
/*-----------------------------CONNECTIVITY CODE----------------------------*/
error_reporting(E_ERROR | E_PARSE); //optional,to remove warning messages.
$conn=mysqli_connect("localhost","root","","databasename");
if ($conn)
{
echo "database connected successfully","<br>";
}
else
{
die("Connection Aborted:" . mysqli_connect_error());
}
/*------------------------------DELETE CODE------------------------------*/
if (isset($_POST['BtnDelete']))
{
// $TxtEmail1=$_REQUEST['TxtEmail'];
$TxtEmail1 = $_POST['TxtEmail'];
$qry = "SELECT * FROM chlsave WHERE TxtEmail4 = '$TxtEmail1'";
$result = mysqli_query($conn, $qry);
if (mysqli_num_rows($result) > 0)
{
$delete = "DELETE FROM chlsave WHERE TxtEmail4 = '$TxtEmail1'";
if (mysqli_query($conn, $delete))
{
echo "<script>alert('Record deleted successfully');</script>";
}
else
{
echo "<script>alert('Record not deleted');</script>";
}
}
else
{
echo "<script>alert('Record not found');</script>";
}
}
---------------------------- OR -----------------------------
if(isset($_REQUEST['BtnDelete']))
{
// $TxtEmail1=$_REQUEST['TxtEmail'];
$TxtEmail1 = $_POST['TxtEmail'];
$qry="select TxtEmail4 from chlsave where TxtEmail4 ='$TxtEmail1'";
$result=mysqli_query($conn,$qry);
$x=0;
while($row1=mysqli_fetch_array($result))
{
$em=$row1['TxtEmail4 '];
if($em)
{
$value="delete from chlsave where TxtEmail4='$TxtEmail1'";
if($conn->query($value)===TRUE)
{
echo "Record Removed ";
}
$x=$x+1;
}
}
if($x===0)
{
echo "Record Not Found & hence not Removed" . $conn->error;
}
}
mysqli_close($conn); //optional,to close the connection properly.
?>
<!--------------------------HTML DESIGN CODE-------------------------------->
<!doctype html>
<html>
<head>
<title>Codershelpline Code for display html value</title>
</head>
<body>
<form name="form1" id="form2" action="#" method="post" >
<center>
<fieldset style="width: 600px">
<legend>Create New Student Account</legend>
<table>
<tr>
<td>Serial No.</td>
<td>:</td>
<td><input type="text" name="TxtSlno"></td>
</tr>
<tr>
<td>Student Name</td>
<td>:</td>
<td><input type="text" name="TxtSname"></td>
</tr>
<tr>
<td>Address</td>
<td>:</td>
<td>
<textarea cols="35" rows="5" name="TxaAddr"></textarea>
</td>
</tr>
<tr>
<td>Sex</td>
<td>:</td>
<td>
<input type="radio" name="Rdbsex" id="RdbMale" value="Male">Male
<input type="radio" name="Rdbsex" id="RdbFemale" value="Female">Female
<input type="radio" name="Rdbsex" id="RdbOther" value="Other">Other
</td>
</tr>
<tr>
<td>User Name</td>
<td>:</td>
<td><input type="text" name="TxtUname"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input type="password" name="TxtPassword"></td>
</tr>
<tr>
<td>Confirm Password</td>
<td>:</td>
<td><input type="password" name="TxtCpassword"></td>
</tr>
<tr>
<td>DOB</td>
<td>:</td>
<td><input type="date" name="DtpDob"></td>
</tr>
<tr>
<td>Mobile No.</td>
<td>:</td>
<td><input type="Number" name="TxtMobno"></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input type="text" name="TxtEmail"></td>
</tr>
<tr>
<td>Security Questions</td>
<td>:</td>
<td>
<select name="CmbSecQues">
<option value="Choose One">Choose One</option>
<option value="What is Your Favourite Book">What is Your Favourite Book</option>
<option value="What is Your Favourite Teacher">What is Your Favourite Teacher</option>
<option value="What is Your Favourate Place">What is Your Favourate Place</option>
<option value="What is Your Favourate Pets">What is Your Favourate Pets</option>
</select>
</td>
</tr>
<tr>
<td>Security Answer</td>
<td>:</td>
<td><input type="text" name="TxtSecAns"></td>
</tr>
<tr>
<td>Hobbies</td>
<td>:</td>
<td>
<input type="checkbox" name="ChkCricket" value="Cricket">Cricket
<input type="checkbox" name="ChkDance" value="Dance">Dance
<input type="checkbox" name="ChkMusic" value="Music">Music
</td>
</tr>
<tr>
<td>Remarks</td>
<td>:</td>
<td><input type="text" name="TxtRem" value="N/A"></td>
</tr>
<tr>
<td></td>
<td></td>
<td>
<input type="submit" name="BtnSave" value="Save">
<input type="reset" name="BtnReset" value="Reset">
<input type="submit" name="BtnDelete" value="Delete"
onclick="return confirm('Are you sure to delete this record?');">
</td>
</tr>
</table>
</fieldset>
</center>
</form>
</body>
</html>
NB: Run the program and enter the correct email (as primary key) value then press delete button to remove the data/record from the database.
![]()
0 Comments