So I have this PHP app where you can make tasks and delete them. The problem is when I have more than one task, all the delete task links will delete the latest task instead of the task you are trying to delete. For example, the latest task has id=25. I want to delete task with id=18, but upon clicking the delete link it deletes task with id=18.
Here is the code for the tasks view:
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$id = $_SESSION['id'] = $row['id'];
$_SESSION['task'] = $row['task'];
$_SESSION['startdate'] = $row['startdate'];
$_SESSION['enddate'] = $row['enddate'];
//convert session startdate to a php date object
$startdate = new DateTime($_SESSION['startdate']);
$enddate = new DateTime($_SESSION['enddate']);
//echo task properties in a table
print "<div class='task'>";
print "<p><a href='editview.php?id=$id'>Edit Task</a> | <a href='delete.php?id=$id'>Delete Task</a></p>";
print "<p>Start Date: ".$startdate->format('d M Y')."</p>";
print "<p>Date to Complete By: ".$enddate->format('d M Y')."</p>";
print "<p>".$_SESSION['task']."</p>";
print "</div>";
}
And here is the delete function being called (this also happens with the editview function, but they are both the same issue):
session_start();
//remove task from tasks table
$query="DELETE FROM tasks WHERE id=?";
//assign task id to variable id
$id = $_SESSION['id'];
try
{
//prepare the query
$stmt = $dbh->prepare($query);
//bind the id table value to the variable id
$stmt->bindParam(1,$id);
$stmt->execute();
}