In this lesson we'll work with the Eskew well delete statement to remove records from our database we
can see an example of the end result here in our web browser all the records in the database are output
ID using a select statement with output it all the columns in the user's table except for date.
So we have I.D. first name last name and email
notice that all the records are organized into a table next to each record we have a delete button when
the delete button is pressed and when I'm going to click the delete button for the record with I.D.
eight
so before I do in the user's table we can see
that this record over here the third one with the name Jane no
so when I pressed the delete button here I get a success message saying the record was deleted successfully
and we can confirm that the record is no longer there when I press the refresh button
we can see that that record has been deleted from the database
and so that's the purpose of this lesson
so to begin create a file and save it in your HP lessons folder save the file is delete dot P. HP
now we'll go through the delete dot BHP script so you can see how it works.
First we create our database connection
next we create and Eskew will select statement to select the I.D. first name last name and email from
the user's table
next we execute the Eskimo statement and store the results in the result variable so there's really
nothing new here.
We've done all this before now.
Here we close off our BHP script because we're going to create an opening table element and define some
attributes for the table so remember that all the records are stored inside of the table.
So what we're doing is creating that table.
And so the table has a width of 300 pixels border of one cell spacing the one and so padding of 1.
The reason we've closed off this BHP script is because it's easier doing it this way rather than nesting
large blocks of each GMO code inside an echo statement.
So if we close off the PDP script we can create some HMO code Gates or a web browser can understand
each team and code outside of the BHP script now inside our open and close table tag.
We have another BHP script here.
Now this script states if the number of rows inside the table is greater than zero then fetch each row
in reflecting each row using our while loop.
So we've closed off this script now as well because we're going back to HDMI out this block of HDMI
code creates a table row with three columns for each record and remember it's inside the while loop.
So this table row with three columns is going to be created for each record and it's going to keep looping
until there are no more records left in the database OK so this block of code pertains to a single row
with three columns and then that just keeps looping over and over until the end.
The values of each record are then stored in the table cells corresponding to their label so the labels
are I.D. first name last name and email and we can see inside the second column of each row we have
the I.D..
Which comes from the idea row inside our users table.
So it's an echo statement and it just outputs the I.D. number.
Now when the third column we have a delete button and the delete button contains a link to a file called
DL user got BHP a variable called I.D. is also passed along in the you are all in this variable contains
the I.D. number of each record so if we hover our mouse over the delete button look at the bottom of
the web browser and you can see that the link is changing or the I.D. number in the link is changing
everything for each record and that you're going to see the importance of that in just a second.
OK.
And you know the remaining parts of this HDMI all code are just outputting each value inside the user's
tables so first name last name and email
so back to the delete button when we click the delete button
we pass along a variable I.D. to this page.
So this is a new page it's called DL user dot BHP.
So if you're following along here you'll need to create this file as well.
And the file starts with a database connection.
Next we have a variable called I.D..
And this I this variable stores the value of the I.D. we pass the long in the are when the delete button
was clicked so look when I click the delete button here look at our new URL DL user dot BHP question
mark.
And then the data we're trying to pass along so we're passing along a variable called I.D. and this
contains the value of the I.D. pertaining to this record.
Next we create a askew well delete statement and that's what the delete statement looks like.
The delete statement deletes the record from table users where the I.D. equals the value in the idea
variable.
So we can see the I.D. for this record is nine.
If I were to click delete it delete to record with the I.D. nine from our users table.
And so that's how the ESC will delete statement works and to test out your script just type in the the
URL on your local host to the delete IP HP file
and then just you can start deleting records.
So record deleted successfully.
Now if you put the back button and refresh the page you should see that that record disappears
so you can see that it is working.