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 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算