‫All right guys.
‫So now we're going to be able to submit a form and add the post to the database.
‫So there's a file called create dot PHP in your starter files.
‫And it's just HTML.
‫It's a form very similar to to what we did in the post lesson.
‫Now let's create a link to this page.
‫So I'm going to go to the index dot PHP and let's go under.
‫Let's see.
‫We'll go right under the for each and let's create we'll do a div let's say margin top six.
‫And then let's do an A tag with a class of we'll do BG dash blue dash 500.
‫So it's going to be formatted as a button.
‫We'll say text white.
‫Let's do P four.
‫So padding on the x axis, padding on the y axis to say rounded.
‫And.
‫Yeah.
‫And then I just want to add a hover class onto that as well.
‫So after rounded let's do hover colon and then BG dash blue 600.
‫And then on focus we'll do outline dash none.
‫All right.
‫Now as for the link, it's just going to go to create dot PHP and then in for the text in the A tag
‫let's say create post.
‫Okay.
‫Let's see what that looks like.
‫All right.
‫So that's fine.
‫So we click that and it takes us to a form that has a title and a body.
‫It looks very similar to what we did in the in the other lesson there.
‫Now it is pushed down kind of far.
‫That's because I have flexbox.
‫Let's see.
‫Let's go to create I kind of don't like that.
‫So right here we have flex justify center item center.
‫Let's.
‫Let's get rid of item center and screen.
‫You don't have to, but I just would rather it up here.
‫So instead I'm just going to add margin top dash six.
‫Yeah, I think I think that's a little better.
‫Maybe we can do margin top ten.
‫Okay.
‫So right now this is just straight HTML.
‫So there's nothing here.
‫We do have the names which is important.
‫So for for instance the text area with the body has a name of body.
‫And the input with the title has a name of title.
‫So now let's go up to the top here.
‫And we want to make sure that we're looking for a Post request okay.
‫Before we execute any of the code that is for the database for the submit.
‫So let's say if.
‫And this is the same thing we did back when we looked at posts.
‫We're going to say if the server.
‫If the request method.
‫So let's say request underscore method is equal to post.
‫And we want to make sure that the form was submitted.
‫So we can say if is set and then post.
‫Submit.
‫So if that's true, then let's get the title.
‫And I don't want to I don't want to save any HTML tags in the database.
‫So I'm going to use HTML special chars.
‫And then from here we'll pass in post.
‫And title.
‫Okay.
‫And then we'll copy that down and let's do body.
‫And body.
‫Okay, so now we get the data and we can check that out.
‫We can do an echo of title.
‫And body.
‫And just put whatever in here.
‫Submit.
‫And there we go.
‫All right.
‫So we're getting the data that's passed in the form.
‫So the next thing we're going to want to do is insert it into the database.
‫So let's create an SQL query.
‫So I'm going to say insert into post.
‫And we want to insert the title and the body and then the values.
‫And you certainly don't want to do this.
‫You certainly don't want to do title body right, because then what you're doing is taking whatever
‫the user passes in here and putting it directly into your query.
‫So we want to use prepared statements with placeholders.
‫So we're going to change that to colon title colon body.
‫And then we can replace those when we execute.
‫So now let's create the statement so stmt.
‫So you can see really no matter what you're doing selecting inserting updating your formatting it in
‫the same way you're creating your query.
‫You're preparing your statement, you're replacing any placeholders and then you're executing.
‫So for the statement let's say PDO and prepare.
‫And we want to pass in our SQL query.
‫You can call it query too.
‫That's another common convention.
‫And then let's set up params that we're going to pass in for replacement.
‫So here we want to replace title.
‫With title.
‫And body.
‫With body and then we can do our execute so statement.
‫And then we want to execute.
‫And we can pass in params.
‫Okay.
‫And then finally let's redirect.
‫So we'll do header location.
‫Let's go back to index dot php.
‫And we can exit.
‫Okay, so I think.
‫I think that should do it.
‫One other thing we probably want to do is add some validation, but we're not really getting.
‫This isn't a project, right.
‫This is just this is just to get you to understand how to how to deal with the database.
‫So when we get to our real project, we'll do all the validation and all that.
‫All right.
‫So let's just refresh this page, not refresh, but go to this page freshly and then let's add let's
‫say post.
‫What do we have.
‫Three in there.
‫So I'll say post four.
‫This is post four.
‫And then I'm going to submit.
‫And again I forgot my database require.
‫So of course we want to require database dot PHP.
‫All right, let's try that again.
‫I'm going to just reload this.
‫Pose for this is pose for.
‫Okay, let's try it.
‫And there we go.
‫We get redirected to index.php and post for is now there.
‫And if I click on it it takes us to the post PHP page.
‫It's passing in.
‫Up here you can see it's passing in for as the ID, so it's fetching that particular post from the database.
‫All right.
‫Cool.
‫So in the next video we'll look at how to delete posts.