无法从post方法获取SQL查询的值

时间:2016-03-12 20:22:03

标签: php sql post

花了很长时间尝试将删除功能添加到我的待办事项列表中。我可以添加todo到sql,也可以在页面上添加,但无法找到删除我的待办事项的方法。也许有人可以帮助我?这是代码:

<!DOCTYPE html>
<html>
    <head>
        <title>To Do</title>
    </head>

    <body>
        <form action="index.php" method="post">
            <input type="text" name="task" placeholder="New task"/>
            <input type="submit" name="add" value="Add" autocomplete="off" required/>
        </form>  

    <?php 


        if (isset($_POST['add'])) {
            $sql = "INSERT INTO list (todo) VALUES ('$_POST[task]')";
            $conn->query($sql);
        }

        if(isset($_POST['delete'])){


            $sql ="DELETE FROM list WHERE todo={$_POST['todo']}";


            $conn->query($sql);

        }

        $query = "SELECT * FROM list";
        $result = mysqli_query($conn, $query);
    ?>

    <?php 
        while ($row = mysqli_fetch_assoc($result)) { ?>
        <?php echo $row['todo'] ?>            
        <form action='index.php?todo="<?php echo $row['todo']; ?>"'method="post">
        <input type="hidden" name="todo" value="<?php echo $row['todo']; ?>">
        <input type="submit" name="delete" value="Delete">
    </form> <br>  
    <?php }
    $conn->close();
     ?>
    </body>
</html>

0 个答案:

没有答案