public function delete(int $id)
{
$this->jokesTable->delete($id);
header('location: /joke/list');
}
// passing arrays into methods is quite a bad idea
// it would be better to pass all seperate elements of
// the joke as parameters, or use a simple object
// with properties
// Since I don't know the full structure of
// $_POST['joke'] I'm unable to provide that here
public function saveEdit(array $jokeData)
{
$joke = $jokeData;
$joke['jokedate'] = new \DateTime();
$joke['authorId'] = 1;
$this->jokesTable->save($joke);
header('location: /joke/list');
}
// if no id is set you can't call this function - it doesn't make sense
public function edit(int $id)
{
$joke = $this->jokesTable->findById($id);
$title = 'Edit joke';
return ['template' => 'editjoke.html.php',
'title' => $title,
'variables' => [
'joke' => $joke ?? null
]
];
}
The code I’m using is from a SitePoint book (PHP & MySQL: Novice to Ninja). It is from a custom framework which doesn’t work like you’ve suggested here (arguments are not passed in where you have done so).
Apologies. I’ll rephrase the question to aim it at those who have read the book.
I think the super globals $_POSTshould be sanitized before usage.
It would be interesting to run the complete project through Tom Butler’s latest PHP validation tool. The results are comprehensive and should give explicit solutions to the problematic script.
A promising concept but no results for the above… no detection of the errors. Two errors (unrelated to this) were detected which, if memory serves me, Tom fixes as he builds the framework towards completion.