The core of the application will require a book list. We will create a general listing of books with title and description.
- Dependency Management with Composer
- Bootstrap 4 Basics: Building Forms with Bootstrap
- Integrating PHP with Databases: Getting Started with PDO
- CRUD Operations with PHP
- Build a Basic PHP Website: Adding a Basic Form
Or if you want to get even more into SQL databases, try our Beginner SQL Track
Other Ways of Doing Things
Your can also access the global $db variable using the "super global"
You can redirect using the builtin header() function, but the HTTP Foundations redirect fails gracefully and we'll be adding cookies to this redirect a little later.
[MUSIC] 0:00 Welcome back. 0:04 Now that we have an understanding of some of the key features that we'll 0:05 be implementing, 0:08 we're ready to start building the functionality into our website. 0:09 We'll start with the ability to add books to our database. 0:13 I've already set up the basic formm as well as the connection to the database. 0:16 But you'll need to add the functionality to take the form data and 0:20 add it to the database. 0:23 After our form is processed, we'll need to send the user back to the book list and 0:25 display the list of books from the database. 0:30 We'll be using a package called HTTP foundations from symphony for 0:32 the HTTP requests and redirection. 0:37 We'll use Composer to manage this package. 0:40 For more information on forms, databases or composer, 0:43 check the notes associated to this video. 0:46 For this project, we'll be using Workspaces. 0:50 But you can also download the files and work locally if you prefer. 0:52 When you open Workspaces, you'll see that I have set up some of the base files for 0:57 us to begin. 1:02 Including a simple file for each of the main pages on our site. 1:02 I also created our main template. 1:07 I'm using a boot swatch theme, which is based on bootstrap. 1:10 This is only to give you something a little easier on the eyes, but 1:13 leaves plenty of room to make it your own. 1:17 In the inc folder, I've split out the head section, navigation and footer, 1:19 which we include on all our pages. 1:24 Now we need to create a form that a user can fill out 1:26 to add a book to our database. 1:29 Inside the inc folder, I've created a basic form with the fields we'll be using. 1:32 Let's open add.php and include that form on our page. 1:36 BookForm.php. 1:50 Besides including the book form file, we also need to add our form tags. 1:54 Method = post. 2:17 And we'll post the form to procedures addBook.php file. 2:22 We're going to be using the Symphony HTTP Foundation 2:38 package that I've already added for you in a composer.json file. 2:41 This package will give us a lot of great features 2:46 that I'll introduce to you as we need them. 2:49 To install this package into your project, 2:52 you'll need to open up the console in your workspace. 2:54 Type composer install. 3:00 Once our packages have installed, we can close the console. 3:10 I've also set up a little helper in the functions.php file. 3:17 This helper gets the current request called request, 3:21 which just creates a new Symphony request object where we can get posted data. 3:24 In order to use these, we'll need to set up a few things. 3:30 So let's create a Bootstrap file where we can add all the things that we need. 3:33 Require once. 3:48 Vendor/autoload.php. 3:54 We also need to include our functions. 4:07 I've also included a connection file which gives us a PDO connection to our database 4:12 through a variable named db. 4:17 Check the notes associated with this video for 4:19 more details on creating the connection. 4:22 Let's require that file here as well. 4:24 Now we can include a single file on a page but have access to everything we need. 4:34 Let's go ahead and create the addBook file in our procedures folder. 4:39 We start by including our bootstrap file at the top. 4:52 After this, we can now get the title and description from the request object. 5:05 Request, get (title). 5:14 And for our bookDescription, 5:20 request get description. 5:25 Now we're set to work with the database. 5:31 I've created all the tables that we need in this course. 5:34 If you want more information on databases in general or this database in particular, 5:38 you can find more information in the notes associated with this video. 5:43 Let's create a function in the functions file. 5:46 We want to be able to add a book. 5:53 This function should accept two parameters, the title and the description. 5:57 We need our db variable from the connection file. 6:06 We'll need this variable in a lot of our database helper functions. 6:09 Using globals is one of the few ways to do this and for 6:13 our use case, it will be the most basic way. 6:16 To do this, we use the keyword global followed by our variable. 6:19 When we set up our connection to the database, 6:25 the db variable was set on the global scope, which makes it available anywhere. 6:28 However, because we're now inside of a function, we need to tell that function 6:33 to look in the global scope to find the variable db so that we can use it. 6:38 Our book requires one more field, an ownerId. 6:43 We'll fill this out later but for now let's just set it to 0. 6:48 We'll be using prepared statements to add our book to our database and 6:53 we'll wrap our statement in a try catch block. 6:56 Let's re-throw the exception. 7:12 For our query, we're going to INSERT INTO books. 7:21 Name, description and 7:28 owneR_ID And then our VALUES. 7:32 Instead of using the question mark symbol for 7:38 numbered placeholders, we'll use named placeholders. 7:40 Starting with a colon, we call this name, 7:45 description, and ownerId. 7:50 Now we can prepare our statement. 7:58 Next we bind our variables. 8:08 Name = title. 8:17 Description = 8:21 description. 8:27 And ownerId = ownerId. 8:37 And finally, we can execute our statement and return the results. 8:44 Now we can use this new function in our add book procedure. 8:53 We're going to wrap this in a try/catch block so 8:57 that we can redirect back to the add page. 9:00 NewBook, bookTitle, 9:18 and bookDescription. 9:24 For the redirection, 9:32 we use some of the features of the Symfony HTTP Foundation's package for responses. 9:34 Using the response create method from the Symphony package accepts three properties. 9:56 The first of these properties is the body of the response. 10:02 This can be of mixed type. 10:06 For our case, we do not need to return any data inside the body. 10:07 So we can just return null. 10:12 The second of the properties is the HTTP status code. 10:15 Also part of the Symphony package is a list of status codes that can be used in 10:20 the form of a word instead of the magic number. 10:24 Numbers that may not mean anything to us when glancing over the code. 10:28 For our redirect status code, we could put a 302. 10:31 But why not be explicit when stating it by just saying, HTTP_FOUND? 10:35 Meaning that the page was found. 10:45 We can use this with a Symphony component. 10:47 The last property is the headers. 10:59 For our redirect to work, 11:01 we need to tell the browser where to go with a Location header. 11:02 We'll redirect to books.php if our book was added. 11:12 We then need to add response, send and exit. 11:20 If we catch an exception, we'll redirect to the add.php page. 11:27 So let's copy these three lines. 11:31 Let's open a browser and add a book. 11:39 Because we're redirected to the books.php page, we know that the book was added. 11:52 But we still need to pull the books from the database to display on 11:57 the book's page. 12:00
You need to sign up for Treehouse in order to download course files.Sign up