Tip:
Highlight text to annotate it
X
Hello students! I wanted to give you a
quick overview on the correct way
to do our homework
four assignment
Basically to remind you.. the assignment
is to create a
user registration form
that is going to record
data for two different tables. The first table is going to be the USER table
and the second table is going to be the USER_PROFILE table. And this is going to require
you to do two
insert statements into your database tables.
In order for you to do this assignment you need to have first completed last
week's homework three
and set up your databases in the phpMyAdmin tool
so that you can properly insert your
form fields into the database.
For this example i have given you a very streamlined version
of the form
that we're going to be doing
just for the purpose of showing you how to do these multiple
insert statements correctly.
Okay, so I've got my code up here
and you'll notice
at the bottom of the form
is the become a member
and I've got the
form action which is calling the same file that we're working on register.php.
It's a post method -- that's very important.
I only have three fields i have the first name
lastname and the email address.
So i don't have a password
or confirm password or confirm email address or anything like that. Again, this
is very simplified for
the purpose of this example.
Okay, so as you recall with our forms
we have
at the top
we checked to see if
it's a form submission
so if the server request method is "post" - it means that the form
has been filled out
hopefully
and that it's
expecting
to process this form data
and we're going to
check to see if the first name was entered. If it
didn't we're going to start compiling
a list of errors.
Same thing with last name and same thing with the e-mail address.
So i did not make this a sticky form
but i would like it if you would make yours a sticky form.
The part that i really want to show you is what to do
how to handle the database side
of your form.
The important line number forty-four here that says require
so we're not doing an includes we're doing a require
because if this file doesn't exist
then the page will not process
you don't wanna be able to process any of this
if you don't have the my mysql connection.
I told you earlier this week
that you need to create this file this
mysqli_connect.php file
before you do this assignment.
If you have not yet
created that final and make sure that you can connect to your
database that you've set up
through your php page
then you need to stop this video
and go back to the online classroom
and read
how to do that.
It's called
READ THIS FIRST.
I cannot
emphasize enough
that do not continue this until you know that you can connect to your database
and that when you run the
mysqli_connect.php
page, that you don't have any errors.
If you have errors, then you need to go back and and fix them.
Usually its a typo or you didn't spell something right.
For me when i was playing with it, I had forgotten the i after
mysql.
Pay attention to the code
and if you have errors you can fix those.
Okay, so once you know that its working,
then you can start creating
your INSERT query.
So the very first query that we want to do is we want to take all the
information
that we've collected from the form
and put it into the USER table.
Remember
for this purpose of
this example i'm only using these three fields firstname lastname email
and then I'm
inserting the values which are the form values right here.
I am not inserting the user_id.
Remember the user_id is an auto-increment field that we are
expecting the database to insert for us automatically.
As soon as the database runs this statement
INSERT INTO USER and here are the three values that I want you to put in
it's going to automatically take the next
available user_id and
put that
into the database field for user_id.
Now since we also need to enter information into the
USER_PROFILE table
we need to supply it that user_id
along with the other information like the tagline and the biography
and
whatever information that you're putting into the USER_PROFILE table.
After we run the query so this line here
is the actual command
line forty eight
that runs the query line forty-seven just creates the query
So line forty nine says if
and we're calling a function here
mysqli_affected_rows
so it's basically saying
with that query
there should be
only one row that
was created as a result of that insert statement.
So we want to check if
the running of that query produced only one record
that we know that everything worked.
If it produces zero
clearly it didn't work.
Only do this if
one record
was created.
This line...
fifty two
is
where i set
a variable
$uid, so u that's basically shorthand for user id.
Again, I'm calling
a mysqli function called insert_id()
and then i'm
providing it
the database connection so in this case
I'm saying get me the very last
id that was inserted
by
the system
and that would be this INSERT
INTO USER
statement
If that happened to be
row number 3
for user_id number 3 in your user table
then now $uid
would have a value of 3.
So what i'm doing in this sample I'm going to give you the URL for this
assignment
printing a little message
you are now registered in chapter twelve you actually be able to log in and then
i'm providing
the variable
what the user is
just so you can see how that works
and then after i do that
i'm going to create another
query -
INSERT INTO USER_PROFILE
and again
for simplicity sake here
I'm only inserting two fields - i have to provide the user_id
and i'm just
hard coding a tagline in here at this point
so again i'm gonna put my variable
$uid
and then
my little fake data
and then i'm gonna run the query
make sure that that runs
and
we're going to go from there close the database output any errors
things like that
so
to actually run this
page
you can do this
follow along
with my little example here. It's
wcbis.lbcc.edu/~nye_r/register.php
wcbis.lbcc.edu/~nye_r/register.php
let's refresh this and put in
our information
John Smith
and I'm going to click REGISTER
and so here we go -
thank you ! You are now registered
your user_id
is fourteen
so if you look in my
if you look in my database table here
let me log back in
and you look at my USER table
you will see user_id 14 this is the one we just put in john smith
jsmith@lbcc.edu
and if i go back to my database
and look at USER_PROFILE
you will see
there's my user_id 14 this is a test
that is what your
homework 4
assignment should do
it should enter into two different tables
but it needs to grab the user_id
that was
first input into the USER table
and hopefully
using this code remembers this is a video so you can stop and look at this code at
any point
from line
forty
to line seventy three
those are the the lines of codes that you that you're gonna wanna modify
but specifically
line 51 and 52 where you're actually storing that
id number
from the USER table
again if you have any questions
don't hesitate to ask me
in the virtual office
or to email me
I am always here to help you out, so if you get stuck on anything
please let me know and i'll try and
help you
understand
the concepts
clearer. Okay?
thank you and good luck! I'm looking forward to seeing what you do