PHP CRUD : How to delete data from database in PHP MySQL
How to Delete data from database in php
Hie Guys, this is the Final one Part 4- where we are going to delete the record from database in php from the student table. we already have fetched data in table where we have a Delete Button where we click to delete the data.
Step 1: Create a file named delete.php and there is 2 files included to delete this task.(Link Above)
remainder that, this is how it looks: index.php file
<th>
<form action="delete.php" method="post">
<input type="hidden" name="id" value="<?php echo $row['id'] ?>">
<input type="submit" name="delete" class="btn btn-danger" value="DELETE">
</form>
</th>
and the main file delete.php
<?php
$connection = mysqli_connect("localhost","root","");
$db = mysqli_select_db($connection, 'phpcrud');
if(isset($_POST['delete']))
{
$id = $_POST['id'];
$query = "DELETE FROM student WHERE id='$id' ";
$query_run = mysqli_query($connection, $query);
if($query_run)
{
echo '<script> alert("Data Deleted"); </script>';
header("location:index.php");
}
else
{
echo '<script> alert("Data Not Deleted"); </script>';
}
}
?>