Updating Tasks5:40 with Alena Holligan
We're all human, and as humans, we all make mistakes. Updating allows us to fix mistakes or make any other changes that may occur.
Now that our projects allow us to update details, 0:00 let's make those same changes to our tasks. 0:02 Let's start in functions.php and update the add_task function. 0:05 First we need to accept the optional task id. 0:10 Then we can check for that id to see if we're going to add or update the task. 0:15 If our task_id is set, then we're going to update. 0:21 sql equals UPDATE our tasks. 0:27 We'll set our project_id. 0:35 Title. 0:40 Date And time. 0:43 Where task ID Else, 0:52 we're going to be using the Insert. 1:01 Once again the update statement has one more place holder than the insert 1:08 statement. 1:11 So let's add another bind value. 1:13 If ($task_id) bindValue(5, 1:19 equals our $task_id. 1:28 We also need to be able to get our task details. 1:34 So let's copy the get_project function. 1:37 We'll rename project to task. 1:46 Let's also rewrite our SELECT statement to pull fields in the order we specify. 2:05 This will be helpful when pulling those results. 2:19 Our functions are ready, so let's add the link in our task list page. 2:22 To our list item we add a href 2:26 = task.php?id= then we 2:31 use our item $task_id. 2:36 And then finish off our a tag. 2:47 Finally we're ready to update our task page. 2:51 Let's copy of the getID conditional from the project page. 2:57 Let's update the list function to use all the task details. 3:07 Task_id, title, date, time and project_id. 3:14 Then we use get_task, and keep our filter input the same. 3:22 Now we add the task_id to the POST condition. 3:29 Remember this is a number int. 3:50 Then we can use that task_id to pass to our add_task function. 3:55 Next we'll update our header text to show for adding or updating a task. 4:09 If !empty($task_id). 4:21 Then we're updating. 4:28 Else we're adding. 4:35 Finally, we need to add the hidden field to the form. 4:46 Let's copy the code from the project.php file again. 4:49 And change out project for task. 5:02 Let's test out our work in a browser. 5:08 Select Tasks and then choose My New Task. 5:14 Let's update and Submit. 5:22 Oops, let's go back and update the SQL statement. 5:26 Update tasks. 5:32 Let's try that again. 5:35 Great work. 5:37 Our task is updated. 5:38
You need to sign up for Treehouse in order to download course files.Sign up