Updating multiple rows in the database

Discussion in 'PHP' started by adamjblakey, Oct 18, 2008.

  1. #1
    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
     
    adamjblakey, Oct 18, 2008 IP
  2. Krnl

    Krnl Peon

    Messages:
    60
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #2
    This may be a stupid question, but... are you setting the $count variable somewhere before executing the for loop?
     
    Krnl, Oct 18, 2008 IP
  3. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     
    Kyosys, Oct 18, 2008 IP
  4. Ueland

    Ueland Peon

    Messages:
    66
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    Ueland, Oct 18, 2008 IP
  5. Krnl

    Krnl Peon

    Messages:
    60
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Ueland - you look like a dustpuppy :eek:
     
    Krnl, Oct 18, 2008 IP
  6. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #6
    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:
     
    adamjblakey, Oct 18, 2008 IP
  7. Ueland

    Ueland Peon

    Messages:
    66
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Strange that... :D

    BTW: It is smart to use exit(); after a header redirect, the script will not stop executing even when the browser redirects.
     
    Ueland, Oct 18, 2008 IP
  8. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #8
    Can anyone see anything wrong with the code at all?
     
    adamjblakey, Oct 18, 2008 IP
  9. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #9
    you might want to use switch($_GET['action'])
     
    Kyosys, Oct 18, 2008 IP
  10. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #10
    I don't exactly know what you mean by that? Can you elaborate.
     
    adamjblakey, Oct 22, 2008 IP
  11. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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]}'");
     
    Kyosys, Oct 22, 2008 IP