Now let's make sure
we can also submit this post,
and then actually store the updated data in the database.
And, for this, back in the update-post template,
we need to work on that form here,
on the action and the method.
Now the method should be POST because we're sending
user generated data that should be stored in the database,
and whenever you're storing something,
somewhere on the server side,
you typically wanna use a post request here.
But what should be our action, our URL?
Well, here, we have different options,
but I would say one approach that makes sense
is to send the request to posts/, then the post id
that's being edited,
so post.id injected with ejs/edit again.
So the same path that we had for loading this page,
but now reached with help of a post request,
which will, therefore, trigger a different route
because post and get requests don't trigger the same routes
because they are different types of requests.
With that, back in blog.js,
we just have to add a fitting route for this.
Now it's a post request, which you wanna handle,
so we use the post method, here,
and then it's /posts/placeholder/edit.
So the same path as we added here,
but here we had it for get requests,
now we have it for post requests.
Then, of course, we have our function as always.
And, in there, we now wanna really update
the data in the database.
For this, again, I'll write my query
for readability reasons in a separate constant.
And you might remember how updating works
from the last core section.
The SQL statement we need, here, is UPDATE,
then the name of the table, where we wanna change things
like posts, and then the SET keyword,
and then we simply tell SQL which fields should be set
to a new value, and all the fields,
where we don't say anything about,
will keep their existing values.
So, here, we can then set the title, for example,
to a new value, and I'll use a question mark
as a placeholder, which will be replaced
with a concrete value later.
Then, separated by a comma,
I'll set the summary to a new value,
and, again, separated by a comma,
I'll set the body to a new value.
These are the three fields we wanna overwrite
because these are the three pieces of data
which we fetch with the form in update-post.ejs.
So that's the first step, but not everything.
We also need to add a WHERE clause to make it clear
which posts or post should be edited.
And, here, it should be a single post,
which you wanna identify with help of the id,
and, again, I'll use a placeholder, here,
to inject the concrete value, later,
and later is now.
We, again, use db.query and pass our query to it,
and then the second parameter value,
which holds all the concrete values that will be used
as replacements for those question marks.
And, now, here we need more than one value.
Before we always dealt with one value,
like the request.params.id,
now we have four question marks,
and, hence, we need four values here,
and the order needs to be the same.
If you have title, here, as a first question mark,
the title that you wanna inject
has to be the first value in this array.
Now, of course, the data is all coming from
the request body here because we have a form submission,
and, hence, the title is request.body.title.
.title because in the update-post form
this input, here, has a name of title,
and it's these names which are available
as properties on req body.
So, here, we have request.body.title,
then request.body.summary,
and then request.body.content.
Content because, again, that's the name value
I have here on this text area.
Now the fourth value, which we have to pass in,
is the value for this question mark in the WHERE clause,
and that's actually the id, which is part of the path,
so that's actually retrieved with req.params.id.
So what we also did before, for example, here,
when we fetched the data for the edit page.
Now I'll reformat this to make it a bit more readable,
and that's now how I wanna execute my query.
Again, let's add async await to await this query,
and then, once it's done, I will, again,
call redirect to redirect back to /posts,
so to the all posts page.
That is what should happen
after saving the updated post data finished.
And, hence, now with that, if we save all of that
this should be working.
If we go back and reload this Update post page,
and I now add a new text, here, to this post content,
like, It was updated!, and maybe also add a couple
of extra exclamation marks, here, to the title.
If I click Update post,
we see those exclamation marks here, so that worked.
And if I click View post,
I also see my extra paragraph here.
So updating also works,
we also got no error, here, in the terminal,
which is a good sign, and, therefore,
the only remaining step now is to make posts deletable.
Again, try this on your own first if you want to,
we'll implement this feature together in the next lecture.