To delete the data from database table by clicking the hyper link we first need to pass the unique identifier or ID as the GET request parameter in the URL.
E.g <a href="delete.php?id=5">Delete</a>
• In the above code we are going to pass the id with value equal to 5.
• We can get this id value on delete.php page with PHP by $_GET[' ']
or $_REQUEST[''] functions.
$id = $_GET['id'];
We will pass the id into the ' href ' attribute of anchor tag . • First will write the file name e.g ' delete.php ' . • Then will use the question mark symbol ?. • Then will pass the parameters e.g ' id=5 '
So the overall URL link will be like as delete.php?id=5 Note! We can pass multiple parameters in the URL with ' & ' symbol. e.g delete.php?user_id=5&record_id=55<table border='1'>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
<th>Password</th>
<th>Delete</th>
</tr>
<?php
$conn = mysqli_connect('localhost', 'root', '', 'myfirstdatabase');
$sql_query = mysqli_query($conn,"SELECT ! FROM users");
while($fetch_result = mysqli_fetch_assoc($sql_query))
{?>
<tr>
<td><?php echo $fetch_result['first_name']; ?></td>
<td><?php echo $fetch_result['last_name']; ?></td>
<td><?php echo $fetch_result['username']; ?></td>
<td><?php echo $fetch_result['password']; ?></td>
<td><a href="delete.php?id=<?php echo $fetch_result['id']; ?>">Delete</a></td>
</tr>
<?php } ?>
</table>
First Name | Last Name | Username | Password | Delete |
---|---|---|---|---|
Hussain | Khan | Alikhan2018 | Ali12345 | Delete |
Umer | Khan | Umerkhan2018 | 12345 | Delete |
Usman | Khan | UsmanKhan22 | Usman12345 | Delete |
<?php
$conn = mysqli_connect('localhost', 'root', '', 'myfirstdatabase');
$id = $_GET['id']; //Geting the id value from URL parameter id
$sql_query = mysqli_query($conn,"DELETE FROM users WHERE id='".$id."' ");
header("location: show.php"); // Redirecting back to the show.php file after deleting the record.
?>