Hey, Ich baue eine Todo-Website, welche problemlos neue Einträge in die DB einfügen kann, und auch einzelne wieder löschen kann. Wenn ich nun allerdings versuche, mehrere zu löschen, wird trotzdem nur ein Entry gelöscht. Mein Form-Code ist folgender:
Mein PHP-Skript zum Hinzufügen/Löschen so:
Ich hoffe jemand kann mir helfen
HTML-Code:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <div class="col s12"> <div class="input-field inline"> <input name="todo" type="text" class="validate"> <label for="todo">Todo</label> <input id="absenden" type ="submit" class ="btn waves-effect waves-light inline" value = "Hinzufügen"/> </div> </div> <ul id="Liste" class="collection z-depth-1"> <li class="collection-header center"><h4>Todo-Liste</h4></li> <?php try { $db = new PDO("mysql:dbname=todo;host=localhost", "root", ""); }catch (PDOException $e) { echo "Fehler: " . htmlspecialchars($e->getMessage()); exit(); } $stmt = $db->query("Select * from todo"); while($row = $stmt->fetch(PDO::FETCH_ASSOC)){ echo "<li class='collection-item'>" . $row["text"] . "<label class='right'><input type='checkbox' name='entry[]' value='". $row["id"]. "'> <span>Löschen</span></label> </li>" ; } ?> </ul> </form>
PHP-Code:
$isSet = isset($_POST["todo"]);
if($isSet == 1) {
$todo = $_POST["todo"];
}else {}
if($isSet == 1 && $todo != "") {
$sql = "INSERT INTO todo(text) VALUES ('$todo');";
$db->exec($sql);
header("Refresh:0");
}elseif ($isSet == 1 && $todo == "") {
if(isset($_POST['entry'])) {
foreach($_POST["entry"] as $entry)
echo $entry ."<br>";
$sql = "DELETE FROM todo WHERE id = " . $entry . ";";
$db->exec($sql);
header("Refresh:0");
}
}
Kommentar