• Register
Welcome to Techno Smarter QA, Questions, Answers Community
Welcome to Techno Smarter Q&A, where you can ask questions and receive answers from other members of the community. Ask the tech & programming Question that you want. Guests Posting are allowed.
+3 votes
6 views

I just reached to delete data by id. I want to delete a specific row by id in PHP. If I have three rows in my MYSQL database table and I want to delete the row third, how can I delete row third using PHP and MYSQL query. Is it possible to delete rows one by one, not entire table?  The row should be deleted by the id. I am a beginner in PHP. I don't know, how to perform the delete query for specific row delete? 

Example - My MYSQL table structure - 

id      name    email 

1    Jack    jack@gmail.com 

2     John  john@gmail.com 

3     Mack   mack@gmail.com 

I can delete the complete table using a delete query, but I don't want to delete entire rows in my MYSQL database table. I want to delete data by id. How can delete data by id? I want to delete table rows id. 

in PHP by (1.8k points) 1 5 24

2 Answers

+1 vote

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 where clause. The where clause helps to use condition with SQL query. PHP supports 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. 

ago by (6.9k points) 4 20 45
0 votes

Thanks for posting. I assist you here. You can use the Where clause to delete the specific rows by id. The where clause is known as the conditional extension that helps to add a condition in the SQL query. You can delete the MYSQL table row using PHP and MYSQL database. 

Use the query. 

DELETE FROM `record` WHERE id = 1;

Record is an MYSQL table name and we specify the where condition. Use the GET method to hold the URL encoded value. 

$id = $_GET['id'];

$result = mysqli_query($mysqli, "DELETE FROM record WHERE id=$id");

I have a reference for you to delete the data by id in PHP. 

Delete data from the table using PHP and MYSQL database

ago by (5k points) 4 15 45
...