Skip to content

PHP Delete Multiple Records

Github: https://github.com/Furkan-Gulsen/PHP-Projects/tree/master/PHP%20Delete%20multiple%20records


index.php

<?php require_once('connect.php') ?>
<!DOCTYPE html>
<html lang="en">
<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">
  <link rel="stylesheet" type="text/css" href="./style.css">
  <title>Delete Multiple Records</title>
</head>
<body>
  
  <?php

  $query = $dataConnect->prepare('SELECT * FROM people');
  $query->execute();

  $peopleCount = $query->rowCount();
  $record      = $query->fetchAll(PDO::FETCH_ASSOC);
  ?>
  <form action='result.php' method='post'>
    <table id="customers">
      <th>ID</th>
      <th>NAME</th>
      <th>SURNAME</th>
      <th>DELETE</th>
  <?php
  foreach($record as $recordPerson){?>   
      <tr>
        <td><?php echo $recordPerson['id'] ?></td>
        <td><?php echo $recordPerson['name'] ?></td>
        <td><?php echo $recordPerson['surname'] ?></td>
        <td><input type='checkbox' name='delete[]' value='<?php echo $recordPerson['id'] ?>'></td>
      </tr>
  <?php 
  }
  ?>
    </table>
    <button type='submit'>DELETE</button>
  </form>
</body>
</html>

result.php

<?php require_once('connect.php') ?>

<?php 

  function GetFilter($value){
    $step1 = trim($value);
    $step2 = strip_tags($step1);
    $step3 = htmlspecialchars($step2);
    return $step3;
  };

  $GetDelete     = $_POST['delete'];
  $GetDeleteTidy = GetFilter(implode(',', $GetDelete));

  $DELETE = $dataConnect->prepare("DELETE FROM people WHERE id IN ($GetDeleteTidy)");
  $DELETE->execute();

  header('Location:index.php');
  exit();

?>


connect.php

<?php 

try{
  $dataConnect = new PDO('mysql:host=localhost;dbname=multiple;charset=UTF8','root','');
}catch(PDOException $e){
  echo 'ERROR: ' . $e->getMessage();
  die();
};

?>

style.css

body{
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
}

#customers {
  font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 600px;
}

#customers td, #customers th {
  border: 1px solid #ddd;
  padding: 8px;
}

#customers tr:nth-child(even){background-color: #f2f2f2;}

#customers tr:hover {background-color: #ddd;}

#customers th {
  padding-top: 12px;
  padding-bottom: 12px;
  text-align: left;
  background-color: #4CAF50;
  color: white;
}

button{
  width: 600px;
  padding: 10px;
  background: #4CAF50;
  color: #fff;
  font-weight: bold;
  outline: none;
  border: none;
  border-radius: 0px 0px 6px 6px;
  cursor: pointer;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: