A guide on how to create function to delete data from php mysqli database
This blog post is coming base on the countless requests I’ve been receiving from my audience, to teach them on how a user can delete information not needed. This information can be a user account, test, image or anything the user has access to. The able the user delete anything, the user must have access to it, which means it is something the user has control over.
Making it
this personal, the user is not allow to delete anything information or data out
of his reach, for example, a user is permitted to delete things under his
account control and not to delete information meant for administrator.
Therefore,
before user will take any step to delete something, he or she must first login
to an active account. And if you’re still finding it difficult to create a php
user registration and login functions, then you can go back to our previous
posts to check on how to create php registration and login functions. Once you
mastered those two things, then to can head over to this post and continue
learning.
So without
taking much of our precious time, let’s dive into making things work. As usual,
we need to make html form, and in this very html form, we need only a submit
button. This button is what will help us trigger the php mysqli data delete
action.
Related articles:
How to master php valuable and data
How to build php users registration system
How to know the difference between programming and web development
In such case
we do not need to create input field inside the html form that carry the submit
button.
To give a
better understanding, we’ll not mix up the html and php codes together, and we
do not necessarily need css code.
Note: you can style the page to your
visitors or users taste, but to keep things easy and fast, we’re working with
css.
As the case
always be, we’ll start with creating php mysqli database and a table to hold
the information or data we want the user to delete, then we will proceed in
creating the html form with only submit button and next is to write out the
entire php delete function and lastly link it up with the html form so as once
a user hit it, it will trigger the action.
Now that
we’re done with the explanation, it is time to start writing our codes to see
the action in place.
Example on
how to create php mysqli database and table
First open
any of your favorite web browsers and search for “XAMPP” and download and install it. Now run the application and click
the Apache start button and also click the MySQL
start button, then wait for them to be active by showing green color, like the
in the image below.
Next is to
click the MySQL Admin button to
access the xampp control panel, where you can create and delete database and
also do same with table.
Since you already
have access to the xampp control panel, it is time to create your database and
table so we can move on to our next step.
Step 1: when you land on the xampp control
panel home page, watch out for the menu list at the top, you’ll get to see DATABASE which the image below shows,
click it and you will be taking to the final page to create your database.
Step 2: when the next page opens, write the
name you want to give your database and hit the create button as shown on the
image below.
Now you’ve
successfully created your database. It is time to create table inside the new
database you just created.
Step 3: on that very page you enter the
database name and click create, below you’ll get to see all the databases
created under your xampp. Select the database you want to work with and you’ll
be taking next step.
Step 4: next you will see an input field
asking for the name you want to give your table. Don’t enter any name yet, and
do not click the create button, but rather click SQL at the top menu list just after structure. The image below
shows it.
Step 5: this is the final stage in creating
your table. This new page that just opened has an input field, type your php
mysqli table create function to enable xampp to create a table following your
command.
Code example;
CREATE TABLE testtb (
id INT(6) UNSIGNED AUTO_INCREMENT
PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL
)
Once you
copy this piece of code and paste it inside the input field and hit the GO button at the bottom as showing on
the image below.
You just
created a table inside your new database. Now, the database and the table is
what we’ll be working with, so you must not forget their names.
Note: we name our database TESTDB and we name our table TESTTB. Keep these names at the back of
your mind; we’ll repeatedly need them while writing our php functions.
But before
we move on to start creating our html data delete button, let me give you a
little explanation about the code created the table for us.
First, I
instruct xampp to create table with the name TESTTB, the table has four (4) rows example; user ID, first name,
last name and email. Each row is instructed to never be empty when users trying
to register.
Related articles:
How to master php valuable and data
How to build php users registration system
How to know the difference between programming and web development
How to
create html data delete button
Like I
initially stated, to create html information delete button, you don’t need any
input field, you only need a submit button and php script to carry out the job.
So now, we will start by writing html form code with only submit button on it.
Name of the file in which we will be creating the html submit button is
delete-account.php.
Html data delete button
example;
<form action=""
method="POST">
<input type="submit" name="delete-submit"
value="delete"/>
</form>
Before we
move on, give me a minute to explain what the above html code does. As usual,
we have html form opening and closing tags, and then inside of the opening tag,
we’ve the action attribute and the method attribute. I am very sure you know
what these two attribute does, but if they happen to be new to you, then you
should check our older posts to learn everything about html form element.
And in
between the html form opening and closing tags, we still have a submit button
that will help us trigger the php mysqli delete function script.
Note: the method attribute should hold
POST, while the action attribute should left empty till we are done creating
the php action script, then we go back and add the file name inside the action
attribute.
Since we’re
done creating the delete button, it is time to write the code for our php
action script.
How to write
code for php mysqli data delete function
It is now
really getting interesting. We’ve come to our last session to actually make the
delete button work once a user clicks it. The only thing we’ll not work through
in this article is the user registration and login. This is because we’ve already
teach these areas in most of our previous posts. What I am trying to tell you
here is that, before a user can be able to delete a file or his or her own
account, the person must login as an active user.
Php mysqli delete query
code example;
<?php
session_start();
//Db connection
$mydb = new
mysqli("localhost", "root", "", "testdb");
if(isset($_POST['delete-submit']))
{
$su =
$mydb->prepare("DELETE FROM testtb WHERE userid='{$usertid}'");
$su->execute();
}
?>
The above
code should be saving as delete.php. Now it is time we go back to our html form
and add delete.php inside the action attribute.
First we
start a session since the user need to login before performing the delete
action, secondly, we make database connection using the our testdb database
name, then we use if statement to check if the submit button is clicked , and
use the delete query to delete the active user inside the testtb by signifying
the user with ID.-
By following
the above steps, you get to successfully build a php data delete function.






Comments
Post a Comment