1 00:00:00,000 --> 00:00:04,786 [MUSIC] 2 00:00:04,786 --> 00:00:05,754 Welcome back. 3 00:00:05,754 --> 00:00:08,919 Now that we have an understanding of some of the key features that we'll 4 00:00:08,919 --> 00:00:09,763 be implementing, 5 00:00:09,763 --> 00:00:13,470 we're ready to start building the functionality into our website. 6 00:00:13,470 --> 00:00:16,270 We'll start with the ability to add books to our database. 7 00:00:16,270 --> 00:00:20,739 I've already set up the basic formm as well as the connection to the database. 8 00:00:20,739 --> 00:00:23,782 But you'll need to add the functionality to take the form data and 9 00:00:23,782 --> 00:00:25,790 add it to the database. 10 00:00:25,790 --> 00:00:30,212 After our form is processed, we'll need to send the user back to the book list and 11 00:00:30,212 --> 00:00:32,701 display the list of books from the database. 12 00:00:32,701 --> 00:00:37,515 We'll be using a package called HTTP foundations from symphony for 13 00:00:37,515 --> 00:00:40,165 the HTTP requests and redirection. 14 00:00:40,165 --> 00:00:43,293 We'll use Composer to manage this package. 15 00:00:43,293 --> 00:00:46,829 For more information on forms, databases or composer, 16 00:00:46,829 --> 00:00:50,210 check the notes associated to this video. 17 00:00:50,210 --> 00:00:52,528 For this project, we'll be using Workspaces. 18 00:00:52,528 --> 00:00:56,420 But you can also download the files and work locally if you prefer. 19 00:00:57,620 --> 00:01:02,148 When you open Workspaces, you'll see that I have set up some of the base files for 20 00:01:02,148 --> 00:01:02,891 us to begin. 21 00:01:02,891 --> 00:01:07,179 Including a simple file for each of the main pages on our site. 22 00:01:07,179 --> 00:01:10,090 I also created our main template. 23 00:01:10,090 --> 00:01:13,547 I'm using a boot swatch theme, which is based on bootstrap. 24 00:01:13,547 --> 00:01:17,268 This is only to give you something a little easier on the eyes, but 25 00:01:17,268 --> 00:01:19,645 leaves plenty of room to make it your own. 26 00:01:19,645 --> 00:01:24,176 In the inc folder, I've split out the head section, navigation and footer, 27 00:01:24,176 --> 00:01:26,680 which we include on all our pages. 28 00:01:26,680 --> 00:01:29,740 Now we need to create a form that a user can fill out 29 00:01:29,740 --> 00:01:32,108 to add a book to our database. 30 00:01:32,108 --> 00:01:36,389 Inside the inc folder, I've created a basic form with the fields we'll be using. 31 00:01:36,389 --> 00:01:40,400 Let's open add.php and include that form on our page. 32 00:01:50,650 --> 00:01:51,150 BookForm.php. 33 00:01:54,850 --> 00:01:59,429 Besides including the book form file, we also need to add our form tags. 34 00:02:17,676 --> 00:02:18,700 Method = post. 35 00:02:22,105 --> 00:02:25,673 And we'll post the form to procedures addBook.php file. 36 00:02:38,255 --> 00:02:41,721 We're going to be using the Symphony HTTP Foundation 37 00:02:41,721 --> 00:02:46,970 package that I've already added for you in a composer.json file. 38 00:02:46,970 --> 00:02:49,650 This package will give us a lot of great features 39 00:02:49,650 --> 00:02:52,270 that I'll introduce to you as we need them. 40 00:02:52,270 --> 00:02:54,700 To install this package into your project, 41 00:02:54,700 --> 00:02:56,590 you'll need to open up the console in your workspace. 42 00:03:00,404 --> 00:03:06,656 Type composer install. 43 00:03:10,893 --> 00:03:14,030 Once our packages have installed, we can close the console. 44 00:03:17,037 --> 00:03:21,159 I've also set up a little helper in the functions.php file. 45 00:03:21,159 --> 00:03:24,800 This helper gets the current request called request, 46 00:03:24,800 --> 00:03:30,480 which just creates a new Symphony request object where we can get posted data. 47 00:03:30,480 --> 00:03:33,960 In order to use these, we'll need to set up a few things. 48 00:03:33,960 --> 00:03:37,970 So let's create a Bootstrap file where we can add all the things that we need. 49 00:03:48,375 --> 00:03:48,875 Require once. 50 00:03:54,750 --> 00:03:59,564 Vendor/autoload.php. 51 00:04:07,710 --> 00:04:09,580 We also need to include our functions. 52 00:04:12,830 --> 00:04:17,180 I've also included a connection file which gives us a PDO connection to our database 53 00:04:17,180 --> 00:04:19,990 through a variable named db. 54 00:04:19,990 --> 00:04:22,496 Check the notes associated with this video for 55 00:04:22,496 --> 00:04:24,817 more details on creating the connection. 56 00:04:24,817 --> 00:04:26,920 Let's require that file here as well. 57 00:04:34,283 --> 00:04:39,085 Now we can include a single file on a page but have access to everything we need. 58 00:04:39,085 --> 00:04:42,510 Let's go ahead and create the addBook file in our procedures folder. 59 00:04:52,515 --> 00:04:55,130 We start by including our bootstrap file at the top. 60 00:05:05,090 --> 00:05:09,360 After this, we can now get the title and description from the request object. 61 00:05:14,812 --> 00:05:17,162 Request, get (title). 62 00:05:20,939 --> 00:05:25,767 And for our bookDescription, 63 00:05:25,767 --> 00:05:30,040 request get description. 64 00:05:31,961 --> 00:05:34,450 Now we're set to work with the database. 65 00:05:34,450 --> 00:05:38,040 I've created all the tables that we need in this course. 66 00:05:38,040 --> 00:05:43,030 If you want more information on databases in general or this database in particular, 67 00:05:43,030 --> 00:05:46,840 you can find more information in the notes associated with this video. 68 00:05:46,840 --> 00:05:48,840 Let's create a function in the functions file. 69 00:05:53,309 --> 00:05:55,480 We want to be able to add a book. 70 00:05:57,340 --> 00:06:01,500 This function should accept two parameters, the title and the description. 71 00:06:06,171 --> 00:06:08,820 We need our db variable from the connection file. 72 00:06:09,825 --> 00:06:13,460 We'll need this variable in a lot of our database helper functions. 73 00:06:13,460 --> 00:06:16,470 Using globals is one of the few ways to do this and for 74 00:06:16,470 --> 00:06:19,500 our use case, it will be the most basic way. 75 00:06:19,500 --> 00:06:24,790 To do this, we use the keyword global followed by our variable. 76 00:06:25,910 --> 00:06:28,230 When we set up our connection to the database, 77 00:06:28,230 --> 00:06:33,540 the db variable was set on the global scope, which makes it available anywhere. 78 00:06:33,540 --> 00:06:38,010 However, because we're now inside of a function, we need to tell that function 79 00:06:38,010 --> 00:06:43,110 to look in the global scope to find the variable db so that we can use it. 80 00:06:43,110 --> 00:06:48,330 Our book requires one more field, an ownerId. 81 00:06:48,330 --> 00:06:53,300 We'll fill this out later but for now let's just set it to 0. 82 00:06:53,300 --> 00:06:56,980 We'll be using prepared statements to add our book to our database and 83 00:06:56,980 --> 00:06:59,280 we'll wrap our statement in a try catch block. 84 00:07:12,693 --> 00:07:13,701 Let's re-throw the exception. 85 00:07:21,080 --> 00:07:26,318 For our query, we're going to INSERT INTO books. 86 00:07:28,735 --> 00:07:32,271 Name, description and 87 00:07:32,271 --> 00:07:37,050 owneR_ID And then our VALUES. 88 00:07:38,490 --> 00:07:40,640 Instead of using the question mark symbol for 89 00:07:40,640 --> 00:07:45,070 numbered placeholders, we'll use named placeholders. 90 00:07:45,070 --> 00:07:50,614 Starting with a colon, we call this name, 91 00:07:50,614 --> 00:07:54,470 description, and ownerId. 92 00:07:58,155 --> 00:07:59,820 Now we can prepare our statement. 93 00:08:08,853 --> 00:08:10,890 Next we bind our variables. 94 00:08:17,766 --> 00:08:21,835 Name = title. 95 00:08:21,835 --> 00:08:27,513 Description = 96 00:08:27,513 --> 00:08:33,193 description. 97 00:08:37,190 --> 00:08:41,435 And ownerId = ownerId. 98 00:08:44,540 --> 00:08:47,567 And finally, we can execute our statement and return the results. 99 00:08:53,230 --> 00:08:56,413 Now we can use this new function in our add book procedure. 100 00:08:57,757 --> 00:09:00,165 We're going to wrap this in a try/catch block so 101 00:09:00,165 --> 00:09:02,230 that we can redirect back to the add page. 102 00:09:18,653 --> 00:09:24,189 NewBook, bookTitle, 103 00:09:24,189 --> 00:09:29,730 and bookDescription. 104 00:09:32,960 --> 00:09:34,220 For the redirection, 105 00:09:34,220 --> 00:09:39,080 we use some of the features of the Symfony HTTP Foundation's package for responses. 106 00:09:56,927 --> 00:10:02,710 Using the response create method from the Symphony package accepts three properties. 107 00:10:02,710 --> 00:10:06,065 The first of these properties is the body of the response. 108 00:10:06,065 --> 00:10:07,417 This can be of mixed type. 109 00:10:07,417 --> 00:10:12,400 For our case, we do not need to return any data inside the body. 110 00:10:12,400 --> 00:10:13,670 So we can just return null. 111 00:10:15,600 --> 00:10:19,350 The second of the properties is the HTTP status code. 112 00:10:20,470 --> 00:10:24,820 Also part of the Symphony package is a list of status codes that can be used in 113 00:10:24,820 --> 00:10:28,000 the form of a word instead of the magic number. 114 00:10:28,000 --> 00:10:31,740 Numbers that may not mean anything to us when glancing over the code. 115 00:10:31,740 --> 00:10:35,860 For our redirect status code, we could put a 302. 116 00:10:35,860 --> 00:10:42,164 But why not be explicit when stating it by just saying, HTTP_FOUND? 117 00:10:45,117 --> 00:10:47,820 Meaning that the page was found. 118 00:10:47,820 --> 00:10:50,130 We can use this with a Symphony component. 119 00:10:59,050 --> 00:11:01,178 The last property is the headers. 120 00:11:01,178 --> 00:11:02,765 For our redirect to work, 121 00:11:02,765 --> 00:11:06,530 we need to tell the browser where to go with a Location header. 122 00:11:12,261 --> 00:11:16,650 We'll redirect to books.php if our book was added. 123 00:11:20,175 --> 00:11:27,060 We then need to add response, send and exit. 124 00:11:27,060 --> 00:11:30,920 If we catch an exception, we'll redirect to the add.php page. 125 00:11:31,990 --> 00:11:33,620 So let's copy these three lines. 126 00:11:39,980 --> 00:11:41,760 Let's open a browser and add a book. 127 00:11:52,395 --> 00:11:57,800 Because we're redirected to the books.php page, we know that the book was added. 128 00:11:57,800 --> 00:12:00,950 But we still need to pull the books from the database to display on 129 00:12:00,950 --> 00:12:01,800 the book's page.