Site icon T4Tutorials.com

Update and Delete multiple records together in PHP MYSQL

Update and Delete multiple records together in PHP MYSQL

In this tutorial, we will learn the followings;

  1. Update multiple records together in PHP MySQL
  2. Delete Multiple records together in PHP MySQL 

Update multiple records together in PHP MySQL

connection.php

<?php
$conn =mysqli_connect("localhost","root","","test");
?>

Update multiple records together in PHP MySQL

UpdateMultiple.php

<?php
include 'connection.php';
$sql="SELECT * FROM test_mysql";
$result=mysqli_query($conn,$sql);
/* Count table rows */
$count=mysqli_num_rows($result);
?>
<?php
/* Check if button name "Submit" is active, do this */
if(isset($_POST['Submit']))
{
	$count=count($_POST["id"]);
	
for($i=0;$i<$count;$i++){
$sql1="UPDATE test_mysql SET name='" . $_POST['name'][$i] . "', lastname='" . $_POST['lastname'][$i] . "', email='" . $_POST['email'][$i] . "' WHERE id='" . $_POST['id'][$i] . "'";
$result1=mysqli_query($conn,$sql1);
}
}

echo $count;
mysqli_close($conn);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">

<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>

<?php
while($rows=mysqli_fetch_array($result)){
?>

<tr>
<td align="center">
<input name="id[]" type="text" id="id" value="<?php echo $rows['id']; ?>">

</td>
<td align="center">
<input name="name[]" type="text" id="name" value="<?php echo $rows['name']; ?>">
</td>
<td align="center">
<input name="lastname[]" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>">
</td>
<td align="center">
<input name="email[]" type="text" id="email" value="<?php echo $rows['email']; ?>">
</td>
</tr>

<?php
}
?>

<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>

Delete Multiple records together in PHP MySQL

DeleteMultiple.php

<?php
$host="localhost";
$username="root";
$password="";
$databasename="sample";
$connect=mysqli_connect("localhost","root","","test");
error_reporting(0);
$_REQUEST;

$select =mysqli_query($connect,"SELECT * FROM test_mysql");
?>
<html>
<body>
<div id="wrapper">


<form method="post" action="">
 <table align="center" cellpadding="10" border="1">
 <tr>
  <th></th>
  <th>NAME</th>
  <th>AGE</th>
 </tr>
 <?php
 while ($row=mysqli_fetch_array($select)) 
 {
  echo "<tr>";
   echo "<td><input type='checkbox' name='no[]' value='".$row['id']."'></td><td>".$row['name']."</td><td>".$row['lastname']."</td><td>".$row['email']."</td>";
  echo "</tr>";
 }
 ?>
 </table>
 <input type="submit" name="delete_records" value="Delete Records">
</form>

</div>
</body>
</html>


<?php
if(isset($_POST['delete_records']) && count($_POST['no'])>0)
{
echo count($_POST['no']);
for($i=0;i<count($_POST['no']);$i++)
{
 $row_no=$_POST['no'][$i];
    echo $row_no; 
 mysqli_query($connect,"delete from test_mysql where id=".$row_no."");
}
}
?>

Database view in PHP Myadmin

Figure: delete update multiple rows together in PHP MY SQL
Exit mobile version