Adding Tasks5:55 with Alena Holligan
Similar to the proceeding videos, we'll accept and filter user data, then write that data to the database.
We're already adding projects so 0:00 we can duplicate that work with minimal changes for adding tasks. 0:02 Not only will this save time, it also keeps things consistent, 0:07 which makes it much easier to read the code later. 0:11 Applications are almost never launched once and left on their own. 0:15 You'll need to add features and fix bugs. 0:19 Having code that is easy to read will make both of those jobs much easier. 0:22 >> Let's start in functions.php again. 0:27 This time, we'll copy the add_project function. 0:29 Well rename it add_task. 0:34 Adding a task will take different parameters, so let's set those up. 0:39 First, we need project_ id, then title. 0:43 Then date and time. 0:49 Next, we can change our SQL statement to use those parameters. 0:53 INSERT INTO tasks. 0:56 Project_id, title. 1:01 Date, and time. 1:07 Making sure we add the right number of placeholders. 1:12 And finally, we'll bind those parameters to our query. 1:15 Let's duplicate this first bind twice. 1:19 The first placeholder is project_id. 1:22 Which is an INT. 1:28 So let's change this to PARAM_INT. 1:30 The second placeholder we can leave as title. 1:34 The third placeholder is date, and 1:40 the fourth placeholder is time, which is also an INT. 1:43 Great, our function is ready to go. 1:50 Let's get our task page set up. 1:52 Let's preview the task form in the browser first. 1:55 Choose Add Task and we see our form. 1:58 First we have a drop down for projects. 2:02 And we need to populate this with options from the database. 2:05 We also see that each of these fields is required. 2:09 Now let's open task.php and get to work. 2:13 First, let's populate the project drop down. 2:17 This is pretty much our project list. 2:20 So let's copy the foreach loop from that file. 2:22 Now let's change the list to an option. 2:34 Our value will be our project_id. 2:50 And will display the title. 2:57 Then finish off our option. 3:01 Now we're ready to filter the user input and send it to our add task function. 3:05 Let's go back to project.php and copy the entire request method conditional. 3:10 Let's duplicate this first filter line twice. 3:24 And make these project_d, 3:28 title, date and time. 3:32 Project_id and time are both integers. 3:43 So let's change this to NUMBER_INT. 3:47 And this one as well. 3:54 FILTER_SANITIZE_NUMBER_INT. 3:55 Now let's change our error message to check for the required fields. 4:00 Project_id, title, date, and time. 4:12 Project, title, date, and time. 4:22 Next, instead of add_ project, we want to use add_ 4:26 task, And we'll pass in our variables. 4:30 And instead of redirecting to project_list let's redirect to task_list. 4:49 And also change the error message to Could not add task. 4:57 Finally, we need the code to display the error message. 5:01 Let's copy the error message from the project page and 5:08 paste it back into our task page. 5:11 Looks great. 5:16 We're ready to test out the form. 5:19 Back in the browser, let's start by submitting an empty form. 5:21 We see the error message telling us that we need to fill in the required fields. 5:26 So let's do that, except let's not select the project. 5:30 Again, we see the error message letting us know to fill in the required fields. 5:41 And, all the data we did fill in is gone. 5:45 Yikes. What a pain. 5:49 But hey, this is our site, so we can do something about that. 5:51
You need to sign up for Treehouse in order to download course files.Sign up