Skip to content

Instantly share code, notes, and snippets.

@MahbbRah
Created April 26, 2016 16:02
Show Gist options
  • Save MahbbRah/98b8d266edfd52e74005804c1ecf0129 to your computer and use it in GitHub Desktop.
Save MahbbRah/98b8d266edfd52e74005804c1ecf0129 to your computer and use it in GitHub Desktop.
multiple value update in single query
<?php
ob_start();
include('../db_config.php');
$db = database::getInstance();
$sql = "SELECT * FROM computer";
$result = $db->dbc->query($sql);
$count = $result->rowCount();
?>
<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>roll</strong></td>
<td align="center"><strong>S Name</strong></td>
<td align="center"><strong>F name</strong></td>
<td align="center"><strong>M name</strong></td>
</tr>
<?php
while($rows=$result->fetch()){
?>
<tr>
<td align="center">
<? $id[]=$rows['roll']; ?><? echo $rows['roll']; ?>
</td>
<td align="center">
<input name="name[]" type="text" id="name" value="<? echo $rows['s_name']; ?>">
</td>
<td align="center">
<input name="lastname[]" type="text" id="lastname" value="<? echo $rows['f_name']; ?>">
</td>
<td align="center">
<input name="email[]" type="text" id="email" value="<? echo $rows['m_name']; ?>">
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if(isset($_POST['Submit'])){
for($i=0;$i<$count;$i++){
$name = $_POST['name'];
$f_name = $_POST['lastname'];
$m_name = $_POST['email'];
$sql1="UPDATE computer SET s_name='$name[$i]', f_name='$f_name[$i]', m_name='$m_name[$i]' WHERE roll='$id[$i]'";
$result1=$db->dbc->query($sql1);
}
if ($result1) {
echo 'success';
header('Location: pdo.php');
}
else { echo "Error";}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment