Cara Membuat Deleted Multiple Baris Menggunakan PHP MySQLi

//lh4.googleusercontent.com/-eAXyxy2JJKA/AAAAAAAAAAI/AAAAAAAAGS4/OHgzqHzCvtQ/s512-c/photo.jpg
September 26, 2018




Selamat pagi kali ini kami akan share bagaimana Cara Membuat Deleted Multiple Baris Menggunakan PHP MySQL.



Perlu kalian ketahui tutorial ini adalah sangat cocok bagi Anda untuk mempelajari dan mengimplementasikan fungsi. tutorial dibawah akan menjelaskan bagaimana cara menghapus beberapa atau seluruhnya data dari database.

Kami menggunakan MYSQLi bersama dengan PHP. Yang pertama kalian harus membuat beberapa file dan setipa file mempunyai fungsi yang berbeda. Fungsi masing-masing dari file akan dijelaskan pad setiap bagia file.

Action.php
Dbconfig.php
Index.php

Buat database dengan nama deleted kemudian masukkan struktur databse berikut.

 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `first_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `last_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `phone` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=Active, 0=Deactive'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `first_name`, `last_name`, `email`, `phone`, `created`, `modified`, `status`) VALUES
(1, 'Arman', 'Basir', 'arman@gmail.com', '123456', '2018-09-18 00:00:00', '2018-09-25 00:00:00', 1),
(2, 'Abi', 'Bidzar', 'abi@codexworld.com', '123456', '2018-09-18 00:00:00', '2018-09-25 00:00:00', 1),
(3, 'Dede bin Salaman', 'Alauddin', 'dede@gmail.com', '123456', '2018-09-18 00:00:00', '2018-09-25 00:00:00', 1),
(4, 'Iad Bin Mutiah', 'Pk ardi', 'iad@gmail.com', '123456', '2018-09-18 00:00:00', '2018-09-25 00:00:00', 1),
(5, 'Lel Bin Mustari', 'Mustari', 'lalili@gmail.com', '123456', '2018-09-18 00:00:00', '2018-09-25 00:00:00', 1);

--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
COMMIT; 

dbConfig.php

File ini berisi kode konfigurasi database yang diperlukan untuk membuat koneksi dengan database.

 <?php
$dbHost = 'localhost';
$dbUser = 'root';
$dbPass = '';
$dbName = 'deleted';
$conn = mysqli_connect($dbHost,$dbUser,$dbPass,$dbName);
if(!$conn){
    die("Database connection failed: " . mysqli_connect_error());
}
?> 

index.php

Kode-kode dalam file ini, membuat tabel yang menunjukkan semua data pengguna. Di sini, setiap baris akan berisi checkbox. Ketika tombol deleted itekan semua data yang dipilih akan dihapus dalam waktu yang bersamaan. Untuk memeriksa semua kotak centang yang dipilih kami menggunakan fungsi JQuery.

 <!DOCTYPE html>
<html>
<head>
    <title>Deleted Multiple</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    
    <!--Script CSS-->
    <link type="text/css" href='https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css' rel='stylesheet'>
    <link type="text/css" href='https://cdn.datatables.net/responsive/2.2.1/css/responsive.dataTables.min.css' rel='stylesheet'>
    <link type="text/css" href='https://cdn.datatables.net/buttons/1.5.1/css/buttons.dataTables.min.css' rel='stylesheet'>
    <script type="text/javascript">
        function delete_confirm(){
            var result = confirm("Are you sure to delete users?");
            if(result){
                return true;
            }else{
                return false;
            }
        }

        $(document).ready(function(){
            $('#select_all').on('click',function(){
                if(this.checked){
                    $('.checkbox').each(function(){
                        this.checked = true;
                    });
                }else{
                 $('.checkbox').each(function(){
                    this.checked = false;
                });
             }
         });

            $('.checkbox').on('click',function(){
                if($('.checkbox:checked').length == $('.checkbox').length){
                    $('#select_all').prop('checked',true);
                }else{
                    $('#select_all').prop('checked',false);
                }
            });
        });
    </script>
</head>
<body>
  <br /><br />
  <div class="container">
     <nav class="navbar navbar-inverse">
        <div class="container-fluid">
           <div class="navbar-header">
              <a class="navbar-brand" href="#">Deleted Multiple</a>
          </div>
      </div>
  </nav>
  <br />
  <h2 align="center">DELETED MULTIPLE</h2>
  <br />
  <?php session_start();
  if(!empty($_SESSION['success_msg'])) {
    ?>

    <div class="alert alert-success"><?php echo $_SESSION['success_msg'];
    ?>
</div>
<?php unset($_SESSION['success_msg']); } ?>
<?php
include_once('dbConfig.php');
$query = mysqli_query($conn,"SELECT * FROM users");
?>
<form name="bulk_action_form" action="action.php" method="post" onSubmit="return delete_confirm();"/>
<table id="example" class="display responsive nowrap" style="width:100%">
    <thead>
        <tr>
            <th><input type="checkbox" name="select_all" id="select_all" value=""/></th>        
            <th>First Name</th>
            <th>Last Name</th>
            <th>Email</th>
            <th>Phone</th>
        </tr>
    </thead>
    <?php
    if(mysqli_num_rows($query) > 0){
        while($row = mysqli_fetch_assoc($query)){
            ?>
            <tbody>
                <tr>
                    <td><input type="checkbox" name="checked_id[]" class="checkbox" value="<?php echo $row['id']; ?>"/></td>        
                    <td><?php echo $row['first_name']; ?></td>
                    <td><?php echo $row['last_name']; ?></td>
                    <td><?php echo $row['email']; ?></td>
                    <td><?php echo $row['phone']; ?></td>
                </tr> 
            </tbody>
        <?php } }else{ ?>
            <tr><td colspan="5">No records found.</td></tr> 
        <?php } ?>
    </table>
    <input type="submit" class="btn btn-danger" name="bulk_delete_submit" value="Delete"/>
</form>
</div>

<!--Script Javascript-->
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/responsive/2.2.1/js/dataTables.responsive.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.1/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.colVis.min.js"></script>
<script>
   $(document).ready(function() {
      $('#example').DataTable( {
        dom: 'Bfrtip',
        buttons: [
        'colvis'
        ]
    } );
  } );
</script>

</body>
</html> 

action.php

Pada file ini, kami membuat kode yang mengambil ID yang akan dihapus. Di dalam satu loop, ia akan mengulangi nilai-nilai dan mengeluarkan query SQL untuk menghapus entri.

 <?php
session_start();
include_once('dbConfig.php');
if(isset($_POST['bulk_delete_submit'])){
    $idArr = $_POST['checked_id'];
    foreach($idArr as $id){
        mysqli_query($conn,"DELETE FROM users WHERE id=".$id);
    }
    $_SESSION['success_msg'] = 'Delete Successful';
    header("Location:index.php");
}
?> 

Sekian share kali ini semoga apa yang kami share bermanfaat bagi anda, Kalian bisa melihat demo dan langsung bisa mendownloadnya.

DEMO          DOWNLOAD

Jika kalian belum paham atau ingin request post atau project silahkan tinggalkan komentar dibawah dan mulai berdiskusi dengan kami.


Related Post

SUBSCRIBE TO OUR NEWSLETTER

closed