So far we've developed cold to view a list of articles view an individual article and insert a new one.
Next we need to be able to edit an existing article let's create a new script file for this and call
it edits Article doppie HP.
First we need to know which article we're going to edit in the individual article script.
We're identifying an article by passing an idea in the query string and then retrieving that article
that has this idea from the database.
We need to do exactly the same thing in the edit script.
However rather than copy and paste this code as we are now repeating ourselves is a bad idea so let's
create a function that gets a single article rijkaard Based on the eidy.
First let's create a file in the includes folder.
To put this function will create a file called Article doppie HP to store all functions related to articles.
It's a good idea to organize functions into files each file containing related functions.
Let's create a function called Get article and will pass in the day of the article we want as an argument
in the individual article script.
We using the query function to select the rickert from the database and were concatenating the I.D.
directly into the query string.
We're providing Escuela injection by checking the I.D. numeric here but for a general function it would
be better to use a prepared statement as we saw earlier.
So in our function first let's create the esky well that we want containing the place holder for the
idea then we'll prepare the Eskew our statement and for this we need the database connection which will
need to pass into this function.
So let's add that as an argument like that.
If the statement is false then an error has occurred.
So we'll print that out.
Otherwise we'll bind the idees parameter to the place holder passing in the I.D. argument and binding
it as an integer then will execute it which returns true if it was successful and then we can get the
result from the statement this resultset represents the results of the ESC U L statement.
But we need to get the data from it in a useful format.
The fetch in a function gets an array of data from a result set by default.
We get an array with both numeric and string indexes.
But if we add a second argument to this we just get an associated array.
This is what we want to return from this function.
If no rijkaard is found this method returns NULL.
So we'll take that into account when we call this function.
Finally let's add a p h.
P duck comment block at the top.
This contains a description of what the function does its arguments and the return value
in our article script.
Now we can call this new function.
First will require the file containing the function.
Then we can replace all of this code with a call to the new function.
As we are now using a prepared statement we no longer need to check that the idea is numeric here as
the prepared statement will protect us from esky well injection.
Let's check this works.
If we click on an individual article it works as before.
Now we can do the same in our edits article script so we'll copy this code that gets the article and
paste it in here for now let's just print out the article.
So on the individual article page we have the idea of a record that exists in the query string.
If I change the u r l to edit article that period p.
We get the article details printed out.
So now we have a generic function we can call to get an individual article.
One thing you might be thinking is that if this function relies on the database connection why dont
we require that function in here directly.
We could even use require once just in case we already required it.
The answer is to keep it simple and easy to maintain.
If you have required statements all over the place you can very quickly get spaghetti code.
Well it's very difficult to follow what's going on and it's also very easy to break something.
If you change one of the included files it's better to keep requires to a minimum.
If you can.