PHPNS Posted June 12, 2007 Share Posted June 12, 2007 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/55270-solved-multiple-mysql-row-updates/ Share on other sites More sharing options...
trq Posted June 12, 2007 Share Posted June 12, 2007 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); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/55270-solved-multiple-mysql-row-updates/#findComment-273174 Share on other sites More sharing options...
PHPNS Posted June 12, 2007 Author Share Posted June 12, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/55270-solved-multiple-mysql-row-updates/#findComment-273205 Share on other sites More sharing options...
trq Posted June 12, 2007 Share Posted June 12, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/55270-solved-multiple-mysql-row-updates/#findComment-273215 Share on other sites More sharing options...
sasa Posted June 12, 2007 Share Posted June 12, 2007 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/55270-solved-multiple-mysql-row-updates/#findComment-273222 Share on other sites More sharing options...
PHPNS Posted June 12, 2007 Author Share Posted June 12, 2007 I got it to work! Thanks for all the suggestions. I was able to get it without trying your code, Sasa, but I will try it anyway. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/55270-solved-multiple-mysql-row-updates/#findComment-273233 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.