Basic MySQLi Commands

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.

Delete row using PHP and MySQL

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.
Читайте также:  Пример использования PHP

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.

    0) < while ($Row = mysqli_fetch_assoc($result)) < //displaying data in table rows dynamically ?>  function DeleteConfirm() > ?>
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

php delete specific row in table

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.

php delete specific row in table

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

This code contains the php query of the application. This code will store the data inputs to the database server. To do that just copy and write this block of codes inside the text editor, then save it as save.php.

Creating the Main Function

This code contains the main function of the application. This code will delete a specific table row when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as delete.php

There you have it we successfully created Delete Specific Row from Table using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Источник

Delete Specific Row From Table in PHP

php delete specific row in table

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.

php delete specific row in table

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

This code contains the php query of the application. This code will store the data inputs to the database server. To do that just copy and write this block of codes inside the text editor, then save it as save.php.

Creating the Main Function

This code contains the main function of the application. This code will delete a specific table row when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as delete.php

There you have it we successfully created Delete Specific Row from Table using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Источник

Tutorial: Easy and Simple Add, Edit, Delete MySQL Table Rows in PHP with Source Code

add edit delete in php

In this tutorial, I will show you how to create a simple add, edit and delete functions in MySQL Table Rows using PHP with MySQLi database. This tutorial does not include a good design but will give you an idea of the essential tasks in PHP and basic queries in MySQLi.

Creating a Database

First, we’re going to create a database that contains our data.

2. Click databases, create a database and name it as “basic_command”.

3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.

CREATE TABLE `user` ( `userid` INT(11) NOT NULL AUTO_INCREMENT, `firstname` VARCHAR(30) NOT NULL, `lastname` VARCHAR(30) NOT NULL, PRIMARY KEY (`userid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

add edit delete in php

Creating a Connection

Next step is to create a database connection and save it as “conn.php”. This file will serve as our bridge between our form and our database. To create the file, open your HTML code editor and paste the code below after the tag.

Creating a Table and Add Form

Next step is to create our table and add form and name it as “index.php”. This table will show the data in our database and the add form will add rows to our database. To create the table and form, open your HTML code editor and paste the code below after the tag.

      

?>
Firstname Lastname
">Edit ">Delete

Creating a Delete Script

Lastly, we create our delete script and name it as “delete.php”. This script will delete the row selected by our user. To create the script, open your HTML code editor and paste the code below after the tag.

Источник

Оцените статью