Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKsenia Breitenmoser
20,874 PointsTask is not deleted
Guys! Please help me to find out a mistake. My code is supposed to delete the Task from the database. But it doesn't.
Here is my code for task_list.php
<?php
require 'inc/functions.php';
$page = "tasks";
$pageTitle = "Task List | Time Tracker";
if (isset($_POST['delete'])) {
if (delete_task(filter_input(INPUT_POST, 'delete', FILTER_SANITIZE_NUMBER_INT))) {
header('location: task_list.php?msg=Task+Deleted');
exit;
} else {
header('location: task_list.php?msg=Unable+to+Delete+Task');
exit;
}
}
if (isset($_GET['msg'])) {
$error_message = trim(filter_input(INPUT_GET, 'msg', FILTER_SANITIZE_STRING));
}
include 'inc/header.php';
?>
<div class="section catalog random">
<div class="col-container page-container">
<div class="col col-70-md col-60-lg col-center">
<h1 class="actions-header">Task List</h1>
<div class="actions-item">
<a class="actions-link" href="task.php">
<span class="actions-icon">
<svg viewbox="0 0 64 64"><use xlink:href="#task_icon"></use></svg>
</span>
Add Task</a>
</div>
<?php
if (isset($error_message)) {
echo "<p class='message'>$error_message</p>";
}
?>
<div class="form-container">
<ul class="items">
<?php
foreach (get_task_list() as $item) {
echo "<li><a href='task.php?id=" . $item['task_id'] . "'>" . $item['title'] . "</a>";
echo "<form method='post' action='task_list.php'>\n";
echo "<input type='hidden' value='" . $item['task_id'] . "' name='delete'/>\n";
echo "<input type='submit' class='button--delete' value='Delete' />\n";
echo "</form>";
echo "</li>";
}
?>
</ul>
</div>
</div>
</div>
</div>
<?php include("inc/footer.php"); ?>
Here is my delete_task function:
function delete_task($task_id) {
include 'connection.php';
$sql = 'DELETE task_id, title, date, time, project_id FROM tasks WHERE task_id = ?';
try {
$results = $db->prepare($sql);
$results->bindValue(1, $task_id, PDO::PARAM_INT);
$results->execute();
} catch (Exception $e) {
echo"Error!: ". $e->getMessage() . "<br />";
return false;
}
return true;
}
very much appreciated!
1 Answer
George Mashasha
11,206 Pointsfirstly i would like to point out that the "delete" SQL statement does not require any fields to be specified ,it should just be : "delete from tasks where id = ? "