Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Sometimes updating is not enough. Sometimes we want to remove these entrees completely. Deleting can be extremely powerful. It can also be extremely dangerous. Let's explore the power and pitfalls as we add the functionality to delete tasks and projects.
Course
Modifying Data with SQL: Removing Data from All Rows in a Table
Incorrect or spam data can mess
with our ability to organize and
0:00
report on important data.
0:04
Most people who have had to deal with
the annoyance of spam in one form or
0:06
another, either emails,
blog comments or physical junk mail.
0:10
I once received fortune cookie spam.
0:15
It read, support literacy,
buy a box of fortune cookies.
0:17
Sometimes updating is not enough.
0:21
Sometimes we want to remove
these entries completely.
0:23
Deleting can be extremely powerful.
0:27
It can also be extremely dangerous.
0:29
Let's explore the power and
0:32
pitfalls as we add the functionality
to delete tasks and projects.
0:34
Lets start in functions.php and
duplicate our get_task function.
0:39
We´ll rename this delete_task.
0:51
Then we update our SQL statement
to use the keyword DELETE.
0:54
The only other thing we need to do
is change our return to return true.
1:03
Everything else,
including the bindValue is ready to go.
1:15
So let's switch over
to our task list page.
1:18
It's a good idea to use $_POST for
1:23
anything that makes
changes to the database.
1:25
So instead of just using a link, we'll add
a form to each item in our foreach loop.
1:28
Method='post'.
1:44
And action='task_list.php'.
1:48
Let's close our form.
2:01
Next, we can add a hidden field for
the task ID.
2:06
Type='hidden', and
2:14
value=$item['task_id'].
2:18
And our name='delete'.
2:31
Then we need a submit button.
2:40
On our submit button,
I'm going to add a class='button--delete'.
2:49
This class is set up so that our button
will look like a simple text link.
2:56
Now we're ready to accept that form data.
3:10
if (isset($_POST['delete']))
3:12
We need to filter input on delete for
our task ID.
3:26
But the only thing we're
doing with that data
3:29
is passing it to our delete_task function,
so we can put that all in one line.
3:32
if (delete_task(,
3:36
(filter_input(INPUT_POST, 'delete',
3:44
FILTER_SANITIZE_NUMBER_INT))).
3:53
If the item is deleted,
we're going to redirect.
4:01
And we're going to set a message.
4:12
Task+Deleted.
4:17
And then make sure we exit.
4:22
Else, we're going to redirect and
4:29
this time set a message that
we are unable to delete.
4:31
Unable +to+Delete+Task'
4:46
And exit again.
4:55
Now let's accept that message and
display it on the screen.
4:57
if (isset($_GET['msg'])),
5:02
Then we're going to set our error message.
5:16
We're going to filter our input.
5:23
(INPUT_GET, 'msg',
5:24
FILTER_SANITIZE_STRING).
5:29
Now let's copy the error message
display from the task page.
5:39
Let's test out the functionality
in the browser.
5:54
Let's delete our updated task.
5:58
Gone!
6:02
[LAUGH] That worked, but
maybe it was a little too easy.
6:04
In the next video,
we'll talk about data integrity
6:08
before adding the ability
to delete projects as well.
6:11
You need to sign up for Treehouse in order to download course files.
Sign up