Up to this point we've created and populated our database directly through HP my admin when creating
web applications.
The database is populated through user interactions with your Web site.
For example the database we created contained registrations for newsletters the users registering for
your newsletter would do so directly through your website in this lesson we're going to explore inputting
data into our database using an H email form is shown in this example the form has three fields first
name last name and email.
We don't have to input the I.D. or date because those two fields are automatically populated the HMO
form can be found in your Downloads folder corresponding to this lesson.
This will save you time from having to code it yourself.
Just remember that if you're using the script provided you must place it in your PDP lessons folder
with the form already created.
We can start right away on the HP script.
So this is what the forum page looks like.
You can see that it's a very basic form.
We have three input fields and they're all nested inside a column the labels are also nested inside
a column.
So we have two columns four rows on the right hand side we have all the input fields on the left hand
side.
We have the labels the form action is set to register dot BHP using the post method and so most of this
again should look familiar.
So now we can start looking at the BHP script.
You need to create this file and save the file is register.
Dot BHP that's very important that you see that the file is not filing because the action will call
this file and also make sure to save this file in the same folder as the form.
So if you're using the same folder we've been using throughout all these exercises then save it in the
P HP lessons folder.
Now you can start coding the script and most of what you see here should look familiar.
The first bit of code starts with storing the inputting data into corresponding variables using post
so the first input value had the name f name.
The second has l name in the third is email
so those input values are stored in these variables f name l name and email will contain the information
that the user has input.
Next we have a function that validates the fields for empty spaces and back and backlashes so remember
these are the three validation methods we explored in a previous lesson.
And we've attached this function to each variable so each each variable is some basic validation next
we establish our connection to the my Web site database game.
That's just using the connection syntax
after the connection is established we have our Eskew will insert statement the format of the insert
statement starts with specifying the table and the columns that the data will be input it into so in
this case we want to place the data into the first name last name and email fields
and that's in the user's table the corresponding values will come from the variables that we defined
earlier.
So name only in email so the F name variable the data in that variable will be stored in the first name
column the L name.
Data will be stored in the last name column and email in email.
We then have an if statement that checks to see if the ESC You will query was successfully executed
if it was the page will output the text.
New record created successfully if not a default error message will be displayed.
And lastly we close the connection to the database using the con closed function.
It's important to close an open database connection to reduce the strain on the My School database server
from too many open connections.
And this is really only an issue if your website gets a lot of traffic.
If you're only having one or two you know a few visitors here and there then it's not going to be a
big deal with our registered IP HP page complete.
Let's go ahead and test the functionality of our form in our web browser so load the You are all to
your form page in your web in your web browser and pull up p HP my admin I've loaded the user's tables
so we can see all the records you're going to notice that as soon as we fill out this form and press
submit a new record is going to be added in our table so I'll just put in the name
and so I'll click Submit
now you'll notice on the right side we have our success message now refresh your table and you'll see
that our record was added to the database right at the end.
The idea was automatically created and the date was also automatically recorded.