So far we've retrieved inserted and deleted records from our table in this lesson we'll show you how
to update records in a table using the ESC You will update statement we'll be creating a script that
allows us to update the first name last name and email of any user in our table first open your delete
dot BHP file and let's add an update link next to our delete link
notice the update link has a variable I.D. which will send the idea value to our update dot P HP page
we'll be creating this in just a moment.
Now if we refresh our delete our HP page you'd have an update link directly underneath the delete link
and that's the only change we need to do on this page.
Now create a new file and save it is update P HP the updated HP file will look like the example shown
here
it will load the first name last name and email of the user corresponding to the I.D. number that was
sent in the U R L from our delete dot P HP page and this is what the format would look like in our you
URL the idea variable reading to the I.D. corresponding to this record.
So in your update dot HP file start by creating a connection to the database so we ever connection variables
the connection string
check the connection
next create an idea variable and get the value using the get I.D. syntax
now create an SPL statement to select the records from the user's table where the I.D. column is equal
to the value stored in our I.D. variable
execute yes you will query now create an if statement in a while loop to go through each record and
store the first name last name and email of each user in a corresponding variable f name l name and
email will store the values from the first name call them last name column and email call after that
initial block of BHP code create your basic h t AML page structure
inside the body type create a form with three inputs and an update button to save time you can copy
and paste the form in your news let Doug P HP file because the form is actually identical we will be
modifying a few attributes
set the form action to update user the IP HP using the post method and we'll create this page and is
the second
for each input create a value attribute for the first name field set the value to the F name variable
for the last name fields added to the L name variable and email for the last for the email input
this will make sure the values are automatically loaded into the input fields when the page is loaded.
So this is very important to do because when we click the update button from our delete page page it's
going to load the information corresponding to the record with this I.D. number and we want these values
to be automatically loaded so the user can just go ahead and make some changes and click the update
button to save them so do that another important thing to do here is create a hidden input with the
name I.D. and value set to the idea variable this will transmit the idea to our next BHP file.
Which will be required.
And so that's basically it for that page next create the update user dot BHP page this page will contain
the script that actually updates a record when the user clicks the update button start by creating your
database connection variables next create your validation function and the four variables that will
store the user inputs from the update form.
So f name will store the value from the F name field.
L name from the only field an email from the email field.
The I.D. comes from our hidden input
now set your database connection check the connection create and ask you will update statement to update
the first name last name and email to the new variable values.
So first name will become the value in the F name variable last name will take on the L name variable
in email will take on the email variable where I.D. is equal to the idea variable so it will only make
the modification to the record that has the correct I.D..
Lastly set up a redirect to your back to your main page.
So we want the user to go back to the deleted page page that lists all the records.
And once you've created all that just go ahead and test your file so load the delete dot page in your
web browser and you'll see all the records that are in your table listed on this page if you need to.
If you've deleted quite a few of your records and you want to add some more you can go ahead and do
so.
But as long as you have even one or two it's fine.
Click on the record you want to update and just add any kind of change so I'll just add a couple of
numbers to the end of each one of these fields and could the update on and so you can see that this
record has been updated.
We'll do another one.
And now this record has also been updated.