This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
We will learn to modify books from the library by modifying our book form.
Functions.php
function deleteBook($bookId) {
global $db;
try {
$stmt = $db->prepare("DELETE from books where id = ? ");
$stmt->bindParam(1, $bookId);
$stmt->execute();
return true;
} catch (\Exception $e) {
return false;
}
}
procedures/deleteBook.php
require_once __DIR__ . '/../inc/bootstrap.php';
$book = getBook(request()->get('bookId'));
deleteBook($book['id']);
$response = \Symfony\Component\HttpFoundation\Response::create(null, \Symfony\Component\HttpFoundation\Response::HTTP_FOUND, ['Location' => '/books.php']);
$response->send();
exit;
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Now that we have our book list
set up where you can add a book.
0:00
What if you want to edit or delete a book?
0:03
At this point we're going
to set up the ability for
0:06
anyone to edit and delete a book.
0:08
But we'll be changing that in a little
bit when we talk about authentication and
0:10
authorization.
0:14
Let's start by opening up
the book.php file in the inc folder.
0:16
We include this file for
each book on our book list page.
0:20
We're going to add two links here.
0:24
Let's start with edit.
0:27
We're going to link this to edit.php and
then pass the bookId.
0:39
We'll copy this line for the delete.
1:00
It will change edit.php
to procedures/deleteBook.
1:08
We're going to create two new files but
let's start by copying the add.php file.
1:17
Create a new file named edit.php.
1:23
We need to set up some book variables so
we can reuse our book form.
1:30
GetBook request get our bookId.
1:42
If we want to use this request,
we'll need to add our bootstrap file.
1:51
Now we can set up our book title.
2:11
We'll also do a book description.
2:22
And finally button text.
2:34
We'll set this to Update Book.
2:38
We also need to change
the visible title on the page.
2:41
Then in our form,
we change the action to edit book.
2:49
We also add the book ID
as a hidden form field.
2:57
We need to create the getBook
function to get this script working.
3:22
In our functions file let's add
a new function called getBook.
3:26
This function needs to accept the book ID.
3:33
We use our global db, and
then a try catch block.
3:40
For our query we're
going to SELECT all FROM
4:09
books WHERE our id = our passed in id.
4:15
Prepare our statement.
4:26
And then we can bind the parameter.
4:30
Then we execute our statement.
4:41
And return the results.
4:50
Since we're calling a single item,
we use the fetch.
4:54
And we also want to tell it to
return an associative array.
5:00
Now let's preview this in a browser.
5:06
Click on the Edit button and
you'll notice that it's not
5:09
actually showing the book
details that we want to edit.
5:12
So let's update the template so
that it reflects the book we're editing.
5:15
All of this will be done
in the book form file.
5:20
In the first input field, let's create
a conditional for the book title.
5:24
If, it is said book title,
5:32
then we echo book title.
5:36
Let's do this same thing for
the description text area.
5:42
The last thing we need
to change is the button.
5:58
We also need to move this
add book into an ELSE block.
6:18
Now let's go back to the browser.
6:31
Now you see the book
reflected in the form.
6:34
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up