dongpo3957 2014-07-27 19:30 采纳率: 0%
浏览 36
已采纳

无法删除特定条目PHP

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();
    }
  • 写回答

2条回答 默认 最新

  • douwei1921 2014-07-27 19:42
    关注

    You have a link:

    <a href='delete.php?id=$id'>
    

    so to get the id you take it from the $_GET not $_SESSION

    $query="DELETE FROM tasks WHERE id=?";
    //assign task id to variable id
    $id = $_GET['id'];
    

    And avoid the Sessions in your loop, its not necessary:

    while($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {       
        $id = $row['id'];
    
        //convert session startdate to a php date object
        $startdate = new DateTime($row['startdate']);
        $enddate = new DateTime($row['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>".$row['task']."</p>";
        print "</div>";
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)