Update Multiple Rows in PHP/MySQL with Checkbox

In this tutorial, we are going to create Update Multiple Rows in PHP/MySQL with Checkbox. This tutorial will teach the user on how to create a simple program in PHP that can update multiple rows using the checkbox as the selector. The feature of this simple source code it can edit multiple data in the database table using a checkbox as a selector. Hope that this tutorial will help you in your future project.

Creating Database and Inserting Data

Our Database

  1. CREATE TABLE `member` (
  2.   `member_id` INT(11) NOT NULL,
  3.   `firstname` VARCHAR(100) NOT NULL,
  4.   `lastname` VARCHAR(100) NOT NULL,
  5.   `middlename` VARCHAR(100) NOT NULL,
  6.   `address` VARCHAR(100) NOT NULL,
  7.   `email` VARCHAR(100) NOT NULL
  8. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Example Data

  1. INSERT INTO `member` (`member_id`, `firstname`, `lastname`, `middlename`, `address`, `email`) VALUES
  2. (1, 'John', 'Meyer', 'Doe', 'United States', '[email protected]'),
  3. (2, 'Jane', 'Doe', 'Meyer', 'United States', '[email protected]'),
  4. (3, 'Andrea', 'Meyer', 'Doe', 'Mexico', '[email protected]'),
  5. (4, 'Rocky', 'Dew', 'Doe', 'Mexico', '[email protected]');

Creating Form Field

Creating a table to view the example data where the user can select using a checkbox as you can see in the image below. Result The source code of the image above.
  1. <form action="edit.php" method="post">
  2.         <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
  3.                 <div class="alert alert-success">
  4.                         <h2 style="text-align:center; font-family:cursive;">Edit Multiple Rows in PHP/MySQL with Checkbox</h2>
  5.                 </div>
  6.                 <thead>
  7.                         <tr>
  8.                                 <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">FirstName</th>
  9.                                 <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">LastName</th>
  10.                                 <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">MiddleName</th>
  11.                                 <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">Address</th>
  12.                                 <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">Email</th>
  13.                                 <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">Action</th>
  14.                         </tr>
  15.                 </thead>
  16.                 <tbody>
  17.                 <?php
  18.                 $query=mysql_query("select * from member")or die(mysql_error());
  19.                 while($row=mysql_fetch_array($query)){
  20.                 $id=$row['member_id'];
  21.                 ?>
  22.                         <tr>
  23.                                 <td style="text-align:center; font-family:cursive; font-size:18px;"><?php echo $row['firstname'] ?></td>
  24.                                 <td style="text-align:center; font-family:cursive; font-size:18px;"><?php echo $row['lastname'] ?></td>
  25.                                 <td style="text-align:center; font-family:cursive; font-size:18px;"><?php echo $row['middlename'] ?></td>
  26.                                 <td style="text-align:center; font-family:cursive; font-size:18px;"><?php echo $row['address'] ?></td>
  27.                                 <td style="text-align:center; font-family:cursive; font-size:18px;"><?php echo $row['email'] ?></td>
  28.                                 <td style="text-align:center; font-family:cursive; font-size:18px;">
  29.                                         <input name="selector[]" type="checkbox" value="<?php echo $id; ?>">
  30.                                 </td>
  31.                         </tr>
  32.                 <?php  } ?>                                              
  33.                 </tbody>
  34.         </table>
  35.         <br />                         
  36.         <button class="btn btn-success pull-right" style="font-family:cursive;" name="submit_mult" type="submit">
  37.                 Update Data
  38.         </button>
  39. </form>

Creating Edit Page

After the user select desired data to update. This page where the user can edit the data to update into the database table as shown in the image below. Result Here's the source code of the image above.
  1. <div class="container">
  2.  
  3. <a href="index.php" style="margin-left: 165px; font-family:cursive;" class="btn btn-danger">Return</a>
  4. <br />
  5. <br />
  6. <form class="form-horizontal" action="edit_save.php" method="post">    
  7. <?php
  8. include('dbcon.php');
  9. $id=$_POST['selector'];
  10. $N = count($id);
  11. for($i=0; $i < $N; $i++)
  12. {
  13. $result = mysql_query("SELECT * FROM member where member_id='$id[$i]'");
  14. while($row = mysql_fetch_array($result))
  15. { ?>
  16.         <div class="thumbnail" style="margin:auto; width:600px;">
  17.         <div style="margin-left: 70px; margin-top: 20px;">
  18.                 <div class="control-group">
  19.                 <label class="control-label" for="inputEmail" style="font-family:cursive; font-weight:bold; font-size:18px; color:blue;">Firstname</label>
  20.                 <div class="controls">
  21.                 <input name="member_id[]" type="hidden" value="<?php echo  $row['member_id'] ?>" />
  22.                         <input name="firstname[]" type="text" style="font-family:cursive; font-weight:bold;" value="<?php echo $row['firstname'] ?>" />
  23.                 </div>
  24.                 </div>
  25.                
  26.                 <div class="control-group">
  27.                 <label class="control-label" for="inputEmail" style="font-family:cursive; font-weight:bold; font-size:18px; color:blue;">Lastname</label>
  28.                 <div class="controls">
  29.                         <input name="lastname[]" type="text" style="font-family:cursive; font-weight:bold;" value="<?php echo $row['lastname'] ?>" />
  30.                 </div>
  31.                 </div>
  32.        
  33.                 <div class="control-group">
  34.                 <label class="control-label" for="inputEmail" style="font-family:cursive; font-weight:bold; font-size:18px; color:blue;">Middlename</label>
  35.                 <div class="controls">
  36.                         <input name="middlename[]" type="text" style="font-family:cursive; font-weight:bold;" value="<?php echo $row['middlename'] ?>" />
  37.                 </div>
  38.                 </div>
  39.                
  40.                 <div class="control-group">
  41.                 <label class="control-label" for="inputEmail" style="font-family:cursive; font-weight:bold; font-size:18px; color:blue;">Address</label>
  42.                 <div class="controls">
  43.                         <input name="address[]" type="text" style="font-family:cursive; font-weight:bold;" value="<?php echo $row['address'] ?>" />
  44.                 </div>
  45.                 </div>
  46.                
  47.                 <div class="control-group">
  48.                 <label class="control-label" for="inputEmail" style="font-family:cursive; font-weight:bold; font-size:18px; color:blue;">Email Address</label>
  49.                 <div class="controls">
  50.                         <input name="email[]" type="text" style="font-family:cursive; font-weight:bold;" value="<?php echo $row['email'] ?>" />
  51.                 </div>
  52.                 </div>
  53.         </div>
  54.         </div>
  55.  
  56.         <br /> 
  57. <?php
  58. }
  59. }
  60. ?>
  61. <input name="" class="btn btn-success" style="margin-left: 165px; font-family:cursive;" type="submit" value="Update">
  62. </form>
  63.  
  64. </div>

Database Name

multi_edit.sql Kindly click the "Download Code" button below for full source code. Thank you very much. Hope that this tutorial will help you a lot. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Comments

if I add a session column and update all record to session 1 then if I want to change it to session 2, I need your help?

Add new comment