Jump to content

[SOLVED] Multiple Mysql Row Updates


PHPNS

Recommended Posts

Hi,  I'm back again, with yet another loop problem.  I'm trying to update multiple rows in a mysql database. I'm having no problem selecting data from the database and displaying it in the form.  I've even found a fairly straight forward script that is supposed to do what I need, BUT, when I submit the form (with changes), the update information is not working.  It appears that the code is failing at/or before the if($Submit){.  FYI, the fields I'm attempting to insert are decimal values (if that matters).  Am I missing something?? Please see the entire script, below:

 

 <?php
require_once ('../mysql_connect.php');

$sql="SELECT * FROM tbl_shipping";
$result=mysql_query($sql);

// Count table rows
$count=mysql_num_rows($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>Overnight</strong></td>
<td align="center"><strong>Economy</strong></td>

</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><? $id[]=$rows['sh_us_id']; ?><? echo $rows['sh_us_id']; ?></td>
<td align="center"><input name="rate_a[]" type="text" id="name" value="<? echo $rows['sh_us_overnight']; ?>"></td>
<td align="center"><input name="rate_b[]" type="text" id="lastname" value="<? echo $rows['sh_us_economy']; ?>"></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 tbl_shipping SET sh_us_overnight='$rate_a[$i]', sh_us_economy='$rate_b[$i]' WHERE id='$id[$i]'";
$result1=mysql_query($sql1);
}
}

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

Link to comment
Share on other sites

You have a whole bunch of undefined variables. Try...

 

<?php

if($_POST['Submit']){
  for ($i=0;$i<$count;$i++){
    $sql1="UPDATE tbl_shipping SET sh_us_overnight='{$_POST['rate_a[$i]']}', sh_us_economy='{$_POST['rate_b[$i]']}' WHERE id='{$_POST['id[$i]']}'";
    $result1=mysql_query($sql1);
  }
}
?>

Link to comment
Share on other sites

Still no luck.  Same result.  Any other suggesstions??  If not, can anyone point me toward a good tutorial on 'inserting' or 'updating' multiple mysql rows?  As you may have seen from my previous posts, it's the loops that tend to mess me up.

Link to comment
Share on other sites

Sorry... my fault, your query should be more like...

 

<?php

if($_POST['Submit']){
  for ($i=0;$i<$count;$i++){
    $sql1="UPDATE tbl_shipping SET sh_us_overnight='{$_POST['rate_a'][$i]}', sh_us_economy='{$_POST['rate_b'][$i]}' WHERE id='{$_POST['id'][$i]}'";
    if (mysql_query($sql1)) {
      echo "success<br />";
    } else {
      echo mysql_error()."<br />$sql1<br />";
    }
  }
}
?>

 

Ive also added some debugging.

Link to comment
Share on other sites

try

 <?php
require_once ('../mysql_connect.php');
// first update database
if($_POST['Submit']){
foreach ($_POST['id'] as $key => $id){
	$rate_a = $_POST['rate_a'][$key];
	$rate_b = $_POST['rate_b'][$key];
	$sql1="UPDATE tbl_shipping SET sh_us_overnight='$rate_a', sh_us_economy='$rate_b' WHERE id='$id'";
	$result1 = mysql_query($sql1) or die(mysql_error());
}
}
$sql="SELECT * FROM tbl_shipping";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($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>Overnight</strong></td>
				<td align="center"><strong>Economy</strong></td>
			</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
			<tr>
				<td align="center"><input type="hidden" name="id[]" value="<?php echo $rows['sh_us_id']; ?>"><?php echo $rows['sh_us_id']; ?></td>
				<td align="center"><input name="rate_a[]" type="text" id="name" value="<? echo $rows['sh_us_overnight']; ?>"></td>
				<td align="center"><input name="rate_b[]" type="text" id="lastname" value="<? echo $rows['sh_us_economy']; ?>"></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($result1){
header("location:index.php");
}
mysql_close();
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.