1 00:00:00,680 --> 00:00:02,820 We're already adding projects so 2 00:00:02,820 --> 00:00:07,380 we can duplicate that work with minimal changes for adding tasks. 3 00:00:07,380 --> 00:00:11,700 Not only will this save time, it also keeps things consistent, 4 00:00:11,700 --> 00:00:15,070 which makes it much easier to read the code later. 5 00:00:15,070 --> 00:00:19,160 Applications are almost never launched once and left on their own. 6 00:00:19,160 --> 00:00:22,180 You'll need to add features and fix bugs. 7 00:00:22,180 --> 00:00:26,520 Having code that is easy to read will make both of those jobs much easier. 8 00:00:27,660 --> 00:00:29,922 >> Let's start in functions.php again. 9 00:00:29,922 --> 00:00:32,700 This time, we'll copy the add_project function. 10 00:00:34,890 --> 00:00:36,920 Well rename it add_task. 11 00:00:39,290 --> 00:00:43,004 Adding a task will take different parameters, so let's set those up. 12 00:00:43,004 --> 00:00:47,595 First, we need project_ id, then title. 13 00:00:49,682 --> 00:00:52,272 Then date and time. 14 00:00:53,540 --> 00:00:56,630 Next, we can change our SQL statement to use those parameters. 15 00:00:56,630 --> 00:01:01,916 INSERT INTO tasks. 16 00:01:01,916 --> 00:01:04,533 Project_id, title. 17 00:01:07,175 --> 00:01:08,685 Date, and time. 18 00:01:12,447 --> 00:01:15,850 Making sure we add the right number of placeholders. 19 00:01:15,850 --> 00:01:19,130 And finally, we'll bind those parameters to our query. 20 00:01:19,130 --> 00:01:20,930 Let's duplicate this first bind twice. 21 00:01:22,657 --> 00:01:26,771 The first placeholder is project_id. 22 00:01:28,813 --> 00:01:30,150 Which is an INT. 23 00:01:30,150 --> 00:01:34,410 So let's change this to PARAM_INT. 24 00:01:34,410 --> 00:01:36,890 The second placeholder we can leave as title. 25 00:01:40,275 --> 00:01:43,649 The third placeholder is date, and 26 00:01:43,649 --> 00:01:48,830 the fourth placeholder is time, which is also an INT. 27 00:01:50,820 --> 00:01:52,910 Great, our function is ready to go. 28 00:01:52,910 --> 00:01:55,040 Let's get our task page set up. 29 00:01:55,040 --> 00:01:57,550 Let's preview the task form in the browser first. 30 00:01:58,960 --> 00:02:02,430 Choose Add Task and we see our form. 31 00:02:02,430 --> 00:02:04,360 First we have a drop down for projects. 32 00:02:05,420 --> 00:02:08,230 And we need to populate this with options from the database. 33 00:02:09,580 --> 00:02:13,060 We also see that each of these fields is required. 34 00:02:13,060 --> 00:02:15,610 Now let's open task.php and get to work. 35 00:02:17,070 --> 00:02:20,080 First, let's populate the project drop down. 36 00:02:20,080 --> 00:02:22,010 This is pretty much our project list. 37 00:02:22,010 --> 00:02:24,330 So let's copy the foreach loop from that file. 38 00:02:34,862 --> 00:02:37,260 Now let's change the list to an option. 39 00:02:50,366 --> 00:02:53,894 Our value will be our project_id. 40 00:02:57,401 --> 00:02:59,070 And will display the title. 41 00:03:01,729 --> 00:03:03,220 Then finish off our option. 42 00:03:05,152 --> 00:03:09,310 Now we're ready to filter the user input and send it to our add task function. 43 00:03:10,520 --> 00:03:14,465 Let's go back to project.php and copy the entire request method conditional. 44 00:03:24,124 --> 00:03:26,670 Let's duplicate this first filter line twice. 45 00:03:28,360 --> 00:03:32,902 And make these project_d, 46 00:03:32,902 --> 00:03:36,693 title, date and time. 47 00:03:43,819 --> 00:03:47,630 Project_id and time are both integers. 48 00:03:47,630 --> 00:03:52,175 So let's change this to NUMBER_INT. 49 00:03:54,320 --> 00:03:55,186 And this one as well. 50 00:03:55,186 --> 00:04:00,340 FILTER_SANITIZE_NUMBER_INT. 51 00:04:00,340 --> 00:04:03,615 Now let's change our error message to check for the required fields. 52 00:04:12,575 --> 00:04:17,252 Project_id, title, date, and time. 53 00:04:22,007 --> 00:04:26,830 Project, title, date, and time. 54 00:04:26,830 --> 00:04:30,500 Next, instead of add_ project, we want to use add_ 55 00:04:30,500 --> 00:04:36,647 task, And we'll pass in our variables. 56 00:04:49,227 --> 00:04:53,372 And instead of redirecting to project_list let's redirect to task_list. 57 00:04:57,142 --> 00:05:00,713 And also change the error message to Could not add task. 58 00:05:01,962 --> 00:05:04,510 Finally, we need the code to display the error message. 59 00:05:08,353 --> 00:05:11,724 Let's copy the error message from the project page and 60 00:05:11,724 --> 00:05:13,783 paste it back into our task page. 61 00:05:16,825 --> 00:05:19,520 Looks great. 62 00:05:19,520 --> 00:05:20,780 We're ready to test out the form. 63 00:05:21,790 --> 00:05:24,840 Back in the browser, let's start by submitting an empty form. 64 00:05:26,400 --> 00:05:30,440 We see the error message telling us that we need to fill in the required fields. 65 00:05:30,440 --> 00:05:33,780 So let's do that, except let's not select the project. 66 00:05:41,162 --> 00:05:45,812 Again, we see the error message letting us know to fill in the required fields. 67 00:05:45,812 --> 00:05:49,940 And, all the data we did fill in is gone. 68 00:05:49,940 --> 00:05:51,390 Yikes. What a pain. 69 00:05:51,390 --> 00:05:54,790 But hey, this is our site, so we can do something about that.