Hi, I have a form with multiple rows and only one update button, i want to update all the rows at once when i press update. I can't see anything wrong with my code: HTML <form action="manage-pages.php?action=update" method="post"> <div id="column"> <ul> <li class="highlight_column">Page Title</li> <li class="highlight_column">Page Name</li> <li class="highlight_column">Order</li> <li class="highlight_column">Show</li> <li class="highlight_column">Options</li> <input name="id[]" type="hidden" id="id[]" value="10"/> <li>Property</li> <li> property.html</li> <li><input name="page_order[]" type="text" class="textfield" id="page_order" style="padding:0px; margin:0px; height:14px;" value="" size="3" maxlength="3"/></li> <li><input name="show[]" type="checkbox" value="1" style="padding:0px; margin:0px;" height="1" checked="checked" /></li> <li><a href="add-page.php?action=edit&id=10">Edit</a> | <a href="add-page-process.php?action=delete&id=10" onclick="javascript:return confirm('Are you sure you want to delete?')">Delete</a></li> <input name="id[]" type="hidden" id="id[]" value="11"/> <li>Contact Us</li> <li> contact-us.html</li> <li><input name="page_order[]" type="text" class="textfield" id="page_order" style="padding:0px; margin:0px; height:14px;" value="" size="3" maxlength="3"/></li> <li><input name="show[]" type="checkbox" value="1" style="padding:0px; margin:0px;" height="1" checked="checked" /></li> <li><a href="add-page.php?action=edit&id=11">Edit</a> | <a href="add-page-process.php?action=delete&id=11" onclick="javascript:return confirm('Are you sure you want to delete?')">Delete</a></li> </ul> <input type="submit" name="Submit" value="Update" class="input2" /> </div> </form> Code (markup): PHP if ($_GET['action'] == 'update'){ for($i=0;$i<$count;$i++){ $sql=mysql_query("UPDATE `pages` SET page_order='$_POST[page_order][$i]', show='$_POST[show][$i]' WHERE id='$_POST[id][$i]'"); } } PHP: It all seems find to me but when i press submit, nothing is updating. Do you have any ideas why they would not be updating? Cheers, Adam
This may be a stupid question, but... are you setting the $count variable somewhere before executing the for loop?
and something unrelated - yet still important - are you sanitizing the POST values somewhere with mysql_real_escape? Otherwise this might lead to an SQL injection
Please say that you do securing of the variables before saving them.. Guessing that $_POST[page_order][0] = "'(TRUNCATE TABLE pages)"; or something like that would create "interesting" results.
Yes sorry i have put: if ($_GET['action'] == 'update'){ $count = 3; $id = intval($_POST['id']); $page_order = clean($_POST['page_order']); $show = clean($_POST['show']); for($i=0;$i<$count;$i++){ $sql=mysql_query("UPDATE `pages` SET page_order='$page_order[$i]', show='$show[$i]' WHERE id='$id[$i]'"); } header("Location:manage-pages.php?action=complete"); } PHP:
Strange that... BTW: It is smart to use exit(); after a header redirect, the script will not stop executing even when the browser redirects.
if you have a bunch of different elseifs questioning $_GET['action'], you could use a switch like so: switch($_GET['action']) { default: //something break; case "something": //something break; case "something1": //something break; case "something2": //something break; case "something3": //something break; } As for your problem, the problem is here: $sql=mysql_query("UPDATE `pages` SET page_order='$page_order[$i]', show='$show[$i]' WHERE id='$id[$i]'"); and the fix is here: $sql=mysql_query("UPDATE `pages` SET page_order='".$page_order[$i]."', show='".$show[$i]."' WHERE id='".$id[$i]."'"); you could also use curly brackets: $sql=mysql_query("UPDATE `pages` SET page_order='{$page_order[$i]}', show='{$show[$i]}' WHERE id='{$id[$i]}'");