- How to delete row using php and mysql
- How to delete a row in MySQL using PHP?
- Add a delete button on every row
- Show a confirmation box before deleting data
- Send the id of the selected row to the “delete.inc.php” page
- How to retrieve id from URL
- Execute the DELETE SQL query
- Delete Specific Row From Table in PHP
- Getting Started:
- Creating Database
- Creating the database connection
- Creating The Interface
- PHP - Delete Specific Row from Table
- System
- Are you sure you want to delete this data?
- Add Member
- Creating the Main Function
- Delete Specific Row From Table in PHP
- Getting Started:
- Creating Database
- Creating the database connection
- Creating The Interface
- PHP - Delete Specific Row from Table
- System
- Are you sure you want to delete this data?
- Add Member
- Creating the Main Function
- Tutorial: Easy and Simple Add, Edit, Delete MySQL Table Rows in PHP with Source Code
- Creating a Database
- Creating a Connection
- Creating a Table and Add Form
- Creating a Delete Script
How to delete row using php and mysql
Welcome back, dear readers! This article is focused on deleting a single row of data in PHP. It’s a simple five-step process to delete a specific row of content from our database. Before learning delete operation, we must know how to insert and fetch data from the database. Once we have fetched the data, we need to create a delete button or link with every row of the fetched data. When a user clicks on the button, the id of the row will be sent to our delete PHP page. On that page, we will use the id to run a MySQL delete query to delete a specific row of data from our database. Now Let’s see the five-step process in detail.
How to delete a row in MySQL using PHP?
- Create a delete button with every row of data fetched from the database.
- When the delete button is clicked, warn the user with a confirmation message.
- Send the id of the row to the “delete.inc.php” page in the URL.
- Fetch the id of the row available in the GET variable sent in URL as a parameter.
- Execute the delete query to delete the specific row of data from our database.
Now let’s discuss all the above-mentioned steps one by one in detail.
Add a delete button on every row
- Create the “viewbook.php” page.
- The first step, as always, is to establish connectivity with your database.
- Now fetch the desired data from the database in a table.
- Create a new column in your table to create your delete button.
- Inside that column, create a button using the HTML anchor tag.
- You can use Bootstrap “btn btn-danger” class to give it a nice red color.
Show a confirmation box before deleting data
- Open a script tag to write a simple javascript function.
- Create a function “DeleteConfirm” inside the script tag.
- Use the javascript confirm() function with a proper message like “Are you sure you want to delete” inside our “DeleteConfirm” function.
- To show the confirmation box, use the “onclick” attribute and call our “DeleteConfirm” function here.
Now when the user clicks the delete button, a confirmation message box will be displayed to the user.
Send the id of the selected row to the “delete.inc.php” page
- First of all, Set the “href” attribute to the Delete page address “includes/delete.inc.php”.
- Then write the question mark “?” to send the name-value pair through GET method. Note that the URL part before question marks is the address and the URL part after the question mark contains the data to be sent.
- Embed PHP and Set the parameter “id” to $Row[‘id’]. Here id is the name and $Row[‘id’] is its value.
The complete code of “Viewbook.php” is visible below.
Title | Author | Description | Date | Download | Update | Delete |
---|---|---|---|---|---|---|
"> Download | Update | Delete |
How to retrieve id from URL
When a user clicks the delete button, he/she will be sent to “delete.inc.php” with the parameter “id”. This parameter contains the id of the selected row in the URL. It’s very easy to fetch it.
- Use $_GET superglobal variable to fetch the value of the parameter from the URL.
- The syntax is also simple. Just write $_GET[‘id’] and store it in a variable.
Execute the DELETE SQL query
- To execute the DELETE query, again we need database connectivity.
- Write a delete query and use the WHERE clause to delete only the row of data selected by the user through id.
- Use php Mysqli_query() function to execute query and store its result in $result variable.
- Now apply a check to see if the query is executed successfully.
- In case of successful execution, send the user back to “viewbook.php” using the header() function.
- But if there is an error, print an error message.
Now check out the complete code of “DeleteBook.inc.php”.
That’s all. We have successfully deleted a single row of data in PHP. I hope you understood the concept and found this article valuable. Here is the end of the article. We will meet again with another informative and interesting article soon.
Delete Specific Row From Table in PHP
In this tutorial, we will create a Delete Specific Row from Table using PHP. This code can delete specific data when the user clicks the delete button. The system uses the MySQLi DELETE query to delete particular data in the table row by providing an id in the WHERE clause. This a user-friendly program. Feel free to modify and use it for your system.
We will be using PHP as a scripting language and interpreter that is mainly used on any web server, including xamp, wamp, etc. It is being applied to any popular websites because of the modern approach as its today.
Getting Started:
First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the XAMPP server https://www.apachefriends.org/index.html .
And, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/ .
Creating Database
Open your database web server then create a database name in it db_delete_table , after that, click Import then locates the database file inside the folder of the application then click ok.
Creating the database connection
Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
Creating The Interface
This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
PHP - Delete Specific Row from Table
Firstname Lastname Address Action ?>