Jump to content

Problem Updating Multiple Rows in MySQL Using PHP


ransy23

Recommended Posts

I am having trouble updating multiple rows in a MySQL database using PHP. (I can update single records, but not several rows at the same time.) When I click "Submit" nothing is updated. Any idea why?

 

<?php

//Connect to DB 
require_once('external_setup.php');
mysql_select_db($database, $db_connect);

//Build and issue query
$qry_result = mysql_query("SELECT * FROM fdic WHERE name = 'Kingston National Bank'") or die(mysql_error());

// Count table rows
$count=mysql_num_rows($qry_result);
?>
<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>City</strong></td>
<td align="center"><strong>Zip</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($qry_result)){
?>
<tr>
<td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
<td align="center"><input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="city[]" type="text" id="city" value="<? echo $rows['city']; ?>"></td>
<td align="center"><input name="zip[]" type="text" id="zip" value="<? echo $rows['zip']; ?>"></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($Submit){
for($i=0;$i<$count;$i++){
$sql1="UPDATE fdic SET name='$name[$i]', city='$city[$i]', zip='$zip[$i]' WHERE id='$id[$i]'";
$result1=mysql_query($sql1);
}
}

if($result1){
header("location:update_multiple2.php");
}
mysql_close();
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.