How To Make Delete Multiple Rows Using PHP
Submitted by alpha_luna on Friday, July 31, 2015 - 14:21.
Language
Good Day!!!
In this tutorial, we are going to learn on How To Make Delete Multiple Rows Using PHP. The features of this project is to add a data then, delete multiple data using check box as our selector. Hope you find this useful.Directions:
For Javascript - Select All Rows
- <script language="JavaScript">
- function sel(source)
- {
- checkboxes = document.getElementsByName('check[]');
- for(var i in checkboxes)
- checkboxes[i].checked = source.checked;
- }
- </script>
For PHP - Add Data
- {
- $userid=$_POST['userid'];
- $name=$_POST['name'];
- $username=$_POST['username'];
- $email=$_POST['email'];
- $address=$_POST['address'];
- $id1 = $_SERVER['REMOTE_ADDR'];
- $insert=mysql_query("insert into user (userid,name,username,email,address) values ('$userid','$name','$username','$email','$address')");
- if($insert)
- {
- echo "<script>alert('User has been added'); window.location='index.php'</script>";
- }
- }
For PHP - Delete Multiple Rows of Data
- {
- $check=$_POST['check'];
- for($i=0;$i<$count;$i++){
- $del_id = $check[$i];
- }
- if($delete)
- {
- echo "<script>alert('User has been deleted'); window.location='index.php'</script>";
- }
- }
Full Source Code
- <!DOCTYPE html>
- <html>
- <head>
- <script language="JavaScript">
- function sel(source)
- {
- checkboxes = document.getElementsByName('check[]');
- for(var i in checkboxes)
- checkboxes[i].checked = source.checked;
- }
- </script>
- <style type="text/css">
- body {
- width:100%;
- font-family:"Trebuchet MS";
- margin:0;
- padding:0;
- }
- h2 a {
- text-decoration:none;
- color:#06F;
- }
- #container {
- width:800px;
- margin:0 auto;
- margin-top:30px;
- }
- </style>
- </head>
- <body>
- <?php
- $db = mysql_connect('localhost','root','') or die ("Unable to connect to Database Server.");
- mysql_select_db ('demo', $db) or die ("Could not select database.");
- if(isset($_POST['submit']))
- {
- $userid=$_POST['userid'];
- $name=$_POST['name'];
- $username=$_POST['username'];
- $email=$_POST['email'];
- $address=$_POST['address'];
- $id1 = $_SERVER['REMOTE_ADDR'];
- $insert=mysql_query("insert into user (userid,name,username,email,address) values ('$userid','$name','$username','$email','$address')");
- if($insert)
- {
- echo "<script>alert('User has been added'); window.location='index.php'</script>";
- }
- }
- if(isset($_POST['delete']))
- {
- $check=$_POST['check'];
- $count=count($check);
- for($i=0;$i<$count;$i++){
- $del_id = $check[$i];
- $delete=mysql_query("delete from user where id='$del_id'") or die(mysql_error());
- }
- if($delete)
- {
- echo "<script>alert('User has been deleted'); window.location='index.php'</script>";
- }
- }
- ?>
- <form method="post" name="form" action="">
- <table border="1" style="border:4px groove #CCC; float:left; margin-left:80px;" cellpadding="4" cellspacing="4">
- <tbody>
- <tr>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- <input name="userid" style="font-size:15px;" placeholder="User ID..." type="text" id="userid" required />
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- <input name="name" style="font-size:15px;" placeholder="Name..." type="text" id="name" required />
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- <input name="username" type="text" style="font-size:15px;" placeholder="Username..." id="username" required />
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- <input name="email" type="email" style="font-size:15px;" placeholder="Email..." id="email" required />
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- <input name="address" style="font-size:15px;" placeholder="Address..." type="text" id="address" required />
- </td>
- </tr>
- <tr>
- <td align="center" colspan="2">
- <input type="submit" style="color:#fff; background-color:#CCC; border:2px groove chocolate; border-radius:4px; font-size:15px;" name="submit" value="Add Employee" id="submit" />
- </td>
- </tr>
- </tbody>
- </table>
- </form>
- <!---Viewing-->
- <form method="post">
- <table border="1" style="border:4px groove #CCC; float:right; margin-right:80px;" cellpadding="0" cellspacing="0" id="container">
- <tr align="center">
- <td>
- </td>
- <td>
- </td>
- <td>
- </td>
- <td>
- </td>
- <td>
- </td>
- <td>
- </td>
- </tr>
- <?php
- $id1 = $_SERVER['REMOTE_ADDR'];
- $result=mysql_query("select * from user order by id ASC ") or die(mysql_error());
- while($user_info=mysql_fetch_array($result)){
- ?>
- <tr align="center">
- <td>
- <input type="checkbox" name="check[]" value="<?php echo $user_info['id']; ?>" id="all" />
- </td>
- <td>
- <?php echo $user_info['userid']; ?>
- </td>
- <td>
- <?php echo $user_info['name']; ?>
- </td>
- <td>
- <?php echo $user_info['username']; ?>
- </td>
- <td>
- <?php echo $user_info['email']; ?>
- </td>
- <td>
- <?php echo $user_info['address']; ?>
- </td>
- </tr>
- <?php } ?>
- <tr>
- <td colspan="6">
- <p align="center">
- <input type="submit" style="color:#fff; background-color:#CCC; border:2px groove chocolate; border-radius:4px; font-size:15px;" name="delete" value="Delete" />
- </p>
- </td>
- </tr>
- </table>
- </form>
- </body>
- </html>
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Comments
Add new comment
- Add new comment
- 8 views