mercredi 20 février 2019

How to perform several SQL queries with different checkbox values as an array or something else

I am currently building a todo-website. I can add todo's to the Dataase, and I can also delete them, but I can only delete once per click on the button. I tried to set the name of the li, as an array, and made a foreach, which I thought would delete every checked checkbox, but I can still delete only ONE entry per click on the button. Here is my full code:

<!DOCTYPE html>
<?php session_start(); ?>
<html lang="de">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Todo-App</title>
  <link rel="stylesheet" href="css/materialize.min.css">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <link rel="stylesheet" href="css/eigenes.css">
</head>
<body>
<div class="container">
  <nav>
    <div class="nav-wrapper z-depth-1">
      <a href="index.php" class="brand-logo center">Todo <i class="material-icons right">add_circle_outline</i></a>
    </div>
  </nav>
<div class="row">
<h3>Todo-App</h3>
<div class="divider"></div>
<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>
</div>
<script src="js/jquery-3.3.1.min.js" charset="utf-8"></script>
<script src="js/materialize.min.js" charset="utf-8"></script>

<script>
if ($("#Liste li").length > 1) {
$("#absenden").val("Hinzufügen/Löschen");
$("#absenden").addClass("blue lighten-3");
}

</script>
</body>
</html>
<?php
// $todo  = isset($_POST["todo"]) && is_string($_POST["todo"]) ? $_POST["todo"] : "";


$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)
    $sql = "DELETE FROM todo WHERE id = " . $entry . ";";
    $db->exec($sql);
    header("Refresh:0");
    echo "Häkchen gesetzt";
  }
}




Aucun commentaire:

Enregistrer un commentaire