‫All right.
‫So now we want to make sure that there is no other user with the same email.
‫And then finally we can submit to the database.
‫Now in this if not empty errors I put an else I actually don't want an else or exiting here and nothing
‫nothing will run past here if there's any errors.
‫So let's check if.
‫Email exists.
‫And.
‫What we're going to do is, is make a query and we're going to select all from the users, where email
‫equals the email that is in the form.
‫So we're going to use a named parameter.
‫So we need to create our params array.
‫And we're going to say email and set that to email.
‫Because remember this email variable is user input data, right?
‫It comes from up here directly from post.
‫So we want to make sure that we that we use named parameters here.
‫And prepared statements.
‫So let's say user and set that to this DB.
‫And we want to make a query.
‫And the query we want to make is select all from users.
‫Where.
‫The email is equal to the email placeholder.
‫And then we want to pass in as a second argument the params array.
‫All right.
‫So once we do that we can check to see if there's a user.
‫So if user then we want to add to the errors array.
‫And we'll just call that email and let's set it to a message.
‫We'll say that email already exists.
‫Yeah.
‫Semicolon.
‫All right.
‫And then we'll load the view.
‫So let's say.
‫Load view.
‫And the view we want to load obviously is users slash create and then we just want to pass in the errors.
‫Okay.
‫And then we'll exit.
‫So we can try that out.
‫Now, as far as the emails I have, if I open up workbench and we go to the users.
‫So I have like user one at gmail, user two, etcetera.
‫So let's try that.
‫I'm just going to put in here user one at gmail.com.
‫And I should see.
‫Hmm.
‫That didn't work.
‫Oh, it didn't work because it checks our other validations first and it doesn't go past this point.
‫So let's let's do everything but the email.
‫So we'll say or not everything but but make the email fail.
‫So 123456123456 email address I'm going to say user one at gmail.com.
‫That email already exists.
‫Okay, now the data in the form is disappearing.
‫So what we can do is just like we did in the listings, right?
‫So if we look in the listings and we look at the value, we have this.
‫All right.
‫So let's let's go to our user.
‫Where is it the users create view.
‫And for the name, I'm going to paste that in and instead of listing, it's going to be user.
‫Remember we passed in from the controller we passed in a user associative array.
‫So I should be able to say user and then brackets name email etcetera.
‫So let's change that to name.
‫So now just to test that out, if I come back to the form and refresh it and I put my name in here,
‫but I fail on other things, my name stays in there, which is what I want.
‫So again, I'm going to just copy this value and we're going to put this here.
‫So email.
‫And then this is city.
‫State.
‫Password.
‫And password confirmation.
‫Actually, no, I'm sorry.
‫We're not doing it for the passwords.
‫They can just, you know, put the password back in.
‫So just to test that out, we'll put something in the name and something in email.
‫Something in the city state, but we'll we won't do the passwords.
‫And now you see that those state filled.
‫All right.
‫Cool.
‫So now we're finally ready to insert the data.
‫So let's go into the user controller and go down here.
‫And let's see we're going to go underneath this if statement here.
‫And let's say create user account.
‫And again we're using prepared statement with named parameters.
‫So let's say params set that to an array and say name.
‫Okay.
‫So we want to do name email.
‫City.
‫State.
‫And we also want to do password.
‫Now obviously we don't want plain text passwords and PHP makes that really easy by letting us use a
‫function called password hash, which I believe uses Bcrypt under the hood.
‫So we can just simply say password underscore hash.
‫And that's going to take in the password.
‫And it's going to take in the the array the options.
‫So you can use like different algorithms and stuff.
‫I'm just going to use password default.
‫Which I think is bcrypt by default.
‫But you see there's like argon and yeah, we'll just do password default.
‫All right.
‫And I think, yeah, that should do it as far as the params go now underneath the params array.
‫Let's make our query.
‫So this db.
‫Query and the query is going to be insert into users.
‫And then we want our fields.
‫So name email.
‫City state.
‫Password.
‫Obviously you're not inserting the password confirmation.
‫And then after that closing parentheses we say values.
‫And that's where we add our placeholders.
‫So colon name.
‫And what have we got?
‫City.
‫State.
‫Uh, what am I missing?
‫Just password.
‫So name.
‫Email.
‫City.
‫State.
‫Password.
‫Name.
‫Email.
‫City.
‫State.
‫Password.
‫Okay.
‫And then we just want to pass in here the params array.
‫And then for now, we're just going to redirect to listings and it's not going to log us in.
‫Like it's not going to create a session or anything, but it should and create the user at least.
‫So let's call redirect.
‫And we'll redirect to slash listings.
‫Or no.
‫Let's just redirect home.
‫Okay, so let's try it out.
‫We're going to come over here.
‫I'm going to go up here and just enter and come to the page freshly.
‫And let's see.
‫So the users I have in the database John Doe.
‫So I'll just add myself.
‫And email address.
‫I'll just say Brad at gmail.
‫City will say.
‫Boston, mass.
‫Password.
‫Okay, so let's try this out.
‫We'll register that email already exists.
‫No, it doesn't, does it?
‫Um.
‫You know that email doesn't exist.
‫Okay, so let's see what I did wrong here.
‫So we're getting the email.
‫Let me just inspect and die.
‫The user.
‫Let's try that.
‫So if I reload the page, it'll resubmit.
‫Oh.
‫Okay, so the problem is that's just the query we need to actually fetch.
‫We need to fetch the user.
‫So now if I refresh that.
‫And I get false because that user doesn't exist.
‫All right.
‫So now it should work.
‫If I get rid of that inspect and die, come back to the page.
‫Brad at gmail.
‫All right, Register and I get redirected.
‫Now let's go to the database and I'm going to click this to select all users.
‫And there it is.
‫And you can see that the password is hashed.
‫Okay.
‫Everything else looks good.
‫Awesome.
‫So we're able to register a user.
‫So I mean it's it's pointless right now.
‫But in the next video we're going to start to work with sessions.
‫We're going to have a session class.
‫And we'll be able to once we submit and validate this create user form, we can then we can create a
‫session.