如何在MysQl数据库中使用复选框删除一个或多个行?

时间:2019-01-07 18:49:31

标签: javascript php mysql

我是编程新手。我希望你们能够在这方面帮助我。那太酷了^^。我在mysql数据库中有一个表,试图使用删除按钮删除数据库复选框中的每一行,我尝试使用Ajax函数以便在不刷新代码的情况下删除带有复选框的行,但它确实似乎不起作用,我也检查了其他帖子,但是解决方案似乎不适用于我想做的事情。所以,这是我的代码:

<!DOCTYPE html>
<html lang="en" >

<head>
  <!-- Encodage des caractères à destination des navigateurs -->
  <meta charset="UTF-8">

  <title>User manager</title>
  
  <!-- Element de lien vers le lien cdn bootstrap css -->
  <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css'>

  
  
</head>

<body>

  <!-- Fixed navbar -->
<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
      <a class="navbar-brand" href="#">Gestion du site</a>
    </div>
    <div id="navbar" class="collapse navbar-collapse">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#about">About</a></li>
      </ul>
    </div>
    <!--/.nav-collapse -->
  </div>
</nav>

<!-- Begin page content -->
<div class="container">
  <div class="page-header">
    <br>
    <h1>Dashboard</h1>
    
    <form class="form-inline" action="">
      <div class="form-group">
        <label for="search">Rechercher Table Trouver:</label>
        <!-- <input type="text" class="form-control" id="user_name" placeholder="search by name"> -->
        <input type="text"  id="myInput" onkeyup="myFunctionTrouver()"  class="table-search" placeholder="Je recherche...">
        <button id="search_button" type="submit" class="btn btn-primary">Soumettre</button>
        <button id="button_delete1" type="button" class="btn btn-danger">Supprimer</button>
        <a class="delete_data" id="<?php echo $row['id']; ?>">Delete</a>
      </div>
    </form>
    
<br>
    <?php
include("displayManagerTrouver.php");
?>



<br>
<br>

<form class="form-inline" action="">
      <div class="form-group">
        <label for="search">Rechercher Table Chercher:</label>
        <!-- <input type="text" class="form-control" id="user_name" placeholder="search by name"> -->
        <input type="text"  id="myInput1" onkeyup="myFunctionChercher()"  class="table-search" placeholder="Je recherche...">
        <button id="search_button" type="submit" class="btn btn-primary">Soumettre</button>
        <button id="btn_delete" type="button" class="btn btn-danger" data-row_id="58">Supprimer</button>
      </div>
    </form>

    <br>

<?php
include("displayManagerChercher.php");
?>


<footer class="footer">
  <div class="container">
    <p class="text-muted"> </p>
  </div>
</footer>


<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js" type="text/javascript"></script>
  
  
<!-- script search bar trouver -->
    <script>
    function myFunctionTrouver() {
  // Declare variables 
  var input, filter, table, tr, td, i, txtValue;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("myTable");
  tr = table.getElementsByTagName("tr");

  // Loop through all table rows, and hide those who don't match the search query
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[3];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    } 
  }
}
    </script>

<!-- script search bar chercher -->
    <script>
    function myFunctionChercher() {
  // Declare variables 
  var input, filter, table, tr, td, i, txtValue;
  input = document.getElementById("myInput1");
  filter = input.value.toUpperCase();
  table = document.getElementById("myTable1");
  tr = table.getElementsByTagName("tr");

  // Loop through all table rows, and hide those who don't match the search query
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[3];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    } 
  }
}

$(document).ready(function(){
 $(".delete_data").click(function(){
   var del_id = $(this).attr('id');
   var parent = $(this).parent();
   $.ajax({
      type:'POST',
      url:'displayManagerTrouver.php',
      data:'delete_id='+del_id,
      success:function(data) {
        if(data) { parent.slideUp(300,function() {
                parent.remove();
        } else { // Error }
      }
   });
 });
});



</body>

</html>
<html>
<?php
// Ajouter la connection a la bd
include "Connection.php";

// Sélectionne les données avec PDO plus des requêtes préparés
echo "<table id='myTable' style='border: solid 1px black;'>";
echo "<tr><th>Prénom</th><th>Nom</th><th>Photo</th><th>Description</th><th>E-mail</th><th>type d'objet</th><th>date de l'objet trouvé</th><th></th></tr>";

// Après avoir selectionné les colonnes de la table voulu il affiche les données dans un tableau HTML
// RecursiveIteratorIterator est utilisé pour itérer sur des itérateurs, récursivement.
class TableRows extends RecursiveIteratorIterator { 
    function __construct($it) { 
        parent::__construct($it, self::LEAVES_ONLY); 
    }

    function current() {
        return "<td style='width: 200px; border: 1px solid black;'>" . parent::current(). "</td>";
    }

    function beginChildren() { 
        echo "<tr>";

    } 

    function endChildren() { 
        echo '<td><input type="checkbox" value = "aria-label"/></td>';
        echo "</tr>" . "\n";
    } 
} 

try {
    // Sélectionne les données à afficher de la base de données trouver
    $stmt = $conn->prepare("SELECT username, lname, photo, objet_description, email, objet_type, date_objet FROM trouver"); 
    $stmt->execute();

     // le tableau resulte en un tableau associatif
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 

    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
        echo $v;
    }
}
catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";


$id = $_POST['checkbox'];
$query = "delete from trouver where ID = $id";

?> 
</html>

在此先感谢您的帮助!

0 个答案:

没有答案