Tip:
Highlight text to annotate it
X
Let’s take a look at editCareer.php. Let’s take a look at editCareer.php.
Before we do that, I did a quick sketch. This shows the layout of the code
When we run the page careerEdit.php
the first thing it does is show
the form. Here’s the form.
You can see that it opened up the database and showed each record
shown the title from the table and it also added in
a delete option button as well as a radio button for each item.
When you click the “Do It” button it is going to check for
the delete button or this option button
and decide what part of the program to run.
As the user goes into the form
the first thing that the page does is open up the database.
Then it shows the form.
I’ll step through that in just a minute.
Then, depending on what the user does,
the submitFlag is set to 2 and that processes the form.
In the function processForm( ) there’s three
parts. The page can either delete the record, add the record
or edit the existing record.
Each of those sets the submitFlag to a different code.
If the person selected the delete option button on the right side of the form
then that will go over and run the deleteRecord( ) function and close the database.
and go back and display the form. If they want to add
a new record, there isn’t an item selected,
and there is no option to delete, then it sets the submitFlag to 5.
Adds in the record with
pre-written data, that the user can change
and then closes the database and
the final option, the user can select an existing item.
They would click one of the left-hand option buttons. That would select
the record they want to edit, setting submitFlag to 3
Close the database and display the new updated records from the table.
You may have noticed that
there is no submitFlag 1. We go from zero to 2
3, 4, 5.
This is one of the things that happens with production code.
You can see here that we have the five
options. If we look at option 1 and see what it is
we will do a CTRL f (find) for validateForm
and you can see that it is in the comments
and it is in the switch, but
that’s the only two places this is mentioned. Whoever wrote this code
never wrote the validateForm( ) function.
They stubbed it in but then they never fleshed it out.
Let’s take a look at the code.
The web page comes in. All of this happens on the same web page.
Standard HTML until we turn the PHP flag on. The first thing we do
is we save the name of this file. We are going to use
this $self variable later on in the form so
the form can call itself. That way it is all automatic
the programmer doesn’t have to worry about having a hard-coded
name in there. The server will automatically keep track of
the file name. We are also setting the $rs (record set)
to null and doing that as a global variable.
There are two ways we can come into a form.
One if the form has never been seen before or this is the first visit
it will just show the form. We will just run showForm( ).
Otherwise, we are going to go through and see what that submitFlag
is and then that number is going to
tell use what to do, based on this diagram that we just looked at.
So, here’s the submitFlag. And, depending on what that submitFlag is
that is what function will run and then
the program will go back and redisplay the form, showing the changes.
Here is our showForm( ). Notice we
got our $self and made it global as well as the $rs (record set)
and also our $conn (connection).
Then we open up the database.
We are going to open and close the database many times in these functions
so what we did is write a function down at the bottom
and you can see here is our openDBF( ) and
we set up our variables. This is nice because
it has all our variables in one place and then we use them
inside the code. When you are maintaining the program this
information has a tendency to change when you change servers.
It is really easy to update here and not miss anything.
We used these variables down here in the actual function doing the work.
Notice that if something goes wrong we display the name of the database
to give us exact details on what is going wrong.
Here is closeDBF( ). Again, we set $rs and $conn to global
we free the record set and just close the database.
This is just a standard
open database.
Let’s go back up to our showForm( )
When we are going to show the form we are basically going to
use SQL to SELECT all the fields (*) from the table pageData
where page equals ‘career’
Remember this table contains information for two different pages.
One is for career listings and the other is for activity listings.
For this page we just want to look at the career items.
If something goes wrong our error message displays the actual SQL statement
so we can see what we are doing. Then we build the form
using telling the user what to do and then
we build the form. This is where the $self variable comes in.
Over here on line #64, when the user clicks on the submit button
then go back and run
this page using the action=“ “ value. It is going to use the POST method
so all the fields in this form (and their values)
are going to be sent in a little suitcase to the server. The server will open it up
and then use that information. That will be the $_POST[ ] array.
Then we set up a to organize the form elements
and here is where we loop through what we got from the database (the record set)
when we opened up the record set on line #64
then this
while loop just goes through each of the rows. So, if there are two rows
or twenty rows or one hundred rows,
this will be displaying new rows on the form.
You can see that there are four rows of data in this table
This is just the basic HTML form here, inserting the values from the table records.
here is our input, our radio buttons for deletion
You can read the comments in this code to see what each of these is doing
and here is our very important hidden field so we know what
value to send back the next time we loop around. We also have
We also added “Add a New Entry” If you look on our form you’ll see it
at the bottom. This isn’t in the database.
This is the loop in the database, and after the loop was finished
then we added on one more row
and we hard-coded in the value “Add a New Entry” and this was our
choice giving it a value of -1.
If you look back on our schematic you can see that
this is what we were looking for: $item == -1
If we have a minus one, then that goes to addRecord( )
So, this is the second flag that we are monitoring.
so the program knows what to do
Then, down here we just close the form, setting up our submit buttons,
A lot of these non-breaking spaces are just for
visual appeal and then, the all important,
we close the database.
When the user clicks on the button
it goes back through here, checks out what flag it is
and we will just go through the schematic. The first flag that we show
is deleteRecord( ) but we have to process the form
so the first time we come around the submitFlag is 2
we process the form, going down here to processForm( )
and this is going to have the three pieces:
Our delete, add new, and
edit existing
These are based on the if( ) statements inside of processForm( ).
These three if statements are summarized on the schematic.
We delete if the optDelete is found.
If someone clicked on one of the option buttons going down the right hand side of the form
then, this code is going to be true. If they didn’t click
on one of those delete, but the item is a -1
then it is automatically known that the user selected a new item
And the final if test is
there is an item and they didn’t click on the delete then
we just edit the existing record that the user selected.
Now, when you add
When you add
a new item to that form, you are going to do it here in showForm( )
and you are going to build up that new item here.
For example, maybe you want to add a checkbox that allows
the user to either mark the item published or not published.
So, that would go right
inside this while loop. Keep in mind that you have to
also add a field in the database table so you
can reflect it in this while loop.
Let’s go down and look at the delete
Now we are handling the form (submitFlag = 2)
and we figure out it is a delete request so we go in here
and we ask, “Are you sure you want to delete this item?”
and we select that from the page and we have the item number
We got the item number from the form.
Here on line #119 we have the item number from the $_POST[ ] array.
that the form sent us.
We know which option choice it is. We grab that item number
and that is the record we want to SELECT.
We show that, displaying all
the content of that record, again, going through a loop
And then we ask the user “Are you sure?” If they click “Yes”
then we send the value 4 back
so it runs the deleteRecord( ) function, and returns
to display the updated table information.
So, we go from the processForm( ) to
deleteRecord( ) function
and we get the item number again (which record in the table)
we set up our SQL statement. In this case we
wrote it in two lines. DELETE FROM pageData (the table)
WHERE item = the specific item number $item.
So, it is only going to delete the one single record.
This is the SQL string. Next, we run openDBF( ) to open up the database
and then we do the deletion
We tell the user what we did,
and when they click the submit button it goes back and
displays the updated table information.
After it shows the page, don’t forget that
it also closes the database. If we go back here to
showForm( )
you can see where closeDBF( ) is run on line #110.
So, again, our schematic shows they deleted it
goes around,
displays the form and closes the database in showForm( ). This schematic is a little
incorrect because this closeDBF( )
actually happens after the form displays.
You should be able to follow the other two options.
We can add new and edit existing.
Let’s just take a look at these two really quick.
When the code comes to add new (#5)
we come to addRecord( ). I’ll do a quick CTRL F (find) to find it in my code...
Here is my addRecord( ) on line #311
We go in and from the $_POST[ ] array
we get all the information from the forms. We store that data
in our variables. Notice how smart the programmer was.
He or she named the fields the same as the table column names.
Then, we are going to add a record and INSERT INTO the table
and then call out each of the field names
and here are the VALUES.
In parenthesis here is ‘career’
we are hard-coding this in because we want this to only show up on the careers page.
Then we put in the information that was extracted from the form.
Once we have this SQL
string built then we run the SQL
and leave a note to the user that we added the new input.
Here is a button just to give the user a chance to read the information. Notice the submitFlag
is back to zero. So, we are going to start all over again on the loop.
Notice that the submit button just says “Click to Continue”
One point here. We didn’t write
this SQL statement all at once. The first thing
we did was to hard-code in
an SQL statement. We wrote “INSERT INTO pageData
page
VALUES
‘career’
and that’s the end.
We saved that as an SQL string.
We ran this hard-coded statement first
Then, one field at a time we added on
header (the table column name) and then hard-coded in a test value
for header. We went in here and said,
header,
‘test1’
Now if you can imagine all of this code
being gone. This hard-coded line is what we tested first.
Do you see what we did? We hard-coded this first to make sure the
single-quotes worked and to make certain this whole SQL
statement was valid.
Then, once we got that.
Usually that is just left there as a comment
to show how the SQL looks. Then we slowly add
each of these column names and hard coded data.
Then, we exchanged the hard-coded data with the variable from the form.
You can see here how we had to use single quotes
These single quotes, right here,
match this single quote…. this value… this single quote.
And then the comma separating the data values.
Notice how we lined up everything so if we made a typo
we would see it right away.
We have already walked through deleteRecord( )
and let’s look at newRecord( ).
Oh, here’s our editRecord( ). Again we
use SQL to select everything for that record that was selected
and we show the values in the form.
This while loop simply loops through the columns for that one record.
The final thing is a new record
and line #169 tests our criteria
and we come in and give the user some shortcuts
but also we filled out default values.
of what is the most common. It is real easy to edit and delete
It is harder for people to come up with consistent entries
on their own. So, when they do add a new record, all of this text
pops up
So, that’s our form that calls itself.
And this is the pattern that it uses.
To repeat: the main place where you want to change your code
is up in the displayForm( ) function.
Now, you’ll also have to go into these other functions
so the processForm( ) and the deleteForm( )
because you will have one or two new fields in your table that you are going to have to account for
when you display the contents of the table.
But, the main part is showForm( ). That is where you
are going to show the form and have all your extra
checkboxes etc, showing people what they can choose.
Also, this is the place where you can add more detail.
So instead of just showing the job title, you can add more information
for each of these. The form will go a little deeper on the screen
but it will sure help the user which ones have been edited
and what they want to do with it. You’ll also have to
go into the database table to see if each record was checked or not
so the user knows if that entry is published or not. So you have to go in and get the information from the table
and display in your checkboxes so the user knows
if the item is published or not published.
So, there you have editCareer.php