0

spent long hours trying to add delete functionality to my to-do list. I can add todo to sql, also it apears on page, but cant find a way to delete my todo. Maybe someone can help me? here's the code:

<!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>
Abdullah Elen
  • 534
  • 4
  • 13
  • What is going wrong? Do you receive an error? – Alexei - check Codidact Mar 12 '16 at 20:27
  • nothing happens, i have no errors but i also cant receive my goal to delete what i want from sql – kobeforthewin Mar 12 '16 at 20:31
  • What values do you have in `$_POST['delete']` and in `$_POST['todo']`? – Alexei - check Codidact Mar 12 '16 at 20:33
  • If todo is a varchar then you need to put single quotes around it in the WHERE clause of the DELETE –  Mar 12 '16 at 20:36
  • @Terminus omg thanks, its working, spent like 3h trying to find a bug like this – kobeforthewin Mar 12 '16 at 20:39
  • No problem. [Just try to be wary of SQL injection](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) that question/answer detail how to avoid it easily. Also, avoid mixing mysqli_ functional programming calls with the OP calls (you have both `$conn->query($sql);` and `mysqli_query($conn, $query);` just use the OP way or not.) –  Mar 12 '16 at 20:45

0 Answers0