Yes, It is possible to delete the MYSQL database row by id. If you delete data only by the table name, it deletes the complete table. This is not so good. You can delete MYSQL table rows using the where clause. The where clause helps to use condition with SQL query. PHP supports the Where clause and you will able specific data by id.
First of all, understand the query -
DELETE FROM `table_name` WHERE id = 1;
DELETE FROM `table_name` WHERE id = 2;
DELETE FROM `table_name` WHERE id = 3;
You need to specify the MYSQL table and where clause with an id number. The id number specifies the unique row number and delete that row by id. You can try this MYSQL query on PHPMYADMIN.
You need to create a MySQL table -
CREATE TABLE `table_name` (
`id` int(40) NOT NULL,
`Name` varchar(40) NOT NULL,
`email` varchar(30) NOT NULL,
);
In the above table, the id column will help you to delete the specific id by number.
The proper way to delete id by PHP -
First of all, you need to select the data and display it on the page with the delete button.
<?php
$host = '127.0.0.1';
$dbName = 'dbhandle';
$dbUsername = 'root';
$dbPassword = '';
$mysqli = mysqli_connect($host, $dbUsername, $dbPassword, $dbame);
$result = mysqli_query($mysqli, "SELECT * FROM table_name ORDER BY id DESC");
?>
<table>
<?php
while($res = mysqli_fetch_array($result)) {
echo '<tr>';
echo "<td>".$res['name']."</td>";
echo "<td>".$res['email']."</td>";
echo"</td><td><a href="display.php?id=$res[id]">Display</a>";
echo"<td> <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you want to delete?')\"><button type='button' class='button'value=''>Delete</button></a></td>";
echo '</tr>';
}
?>
</table>
Now, all data will be display on the page with the delete button. The delete button will be redirected on the delete.php page.
Now, you need to create another delete.php page.
Create a delete.php page.
<?php
$host = '127.0.0.1';
$dbName = 'dbhandle';
$dbUsername = 'root';
$dbPassword = '';
$mysqli = mysqli_connect($host, $dbUsername, $dbPassword, $dbame);
$id = $_GET['id'];
$result = mysqli_query($mysqli, "DELETE FROM tabe_name WHERE id=$id");
?>
The GET method holds the id number and deletes the MYSQL row by id in PHP programming.