Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Location
    Tampa
    Posts
    223
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Updating Multiple Rows

    I am trying to update multiple rows with one submit button.

    I currently have a table that repeats out the following form for each folder in the database:

    Code:
    <form name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
                          <label>
                          <div align="center">
                            <input name="updateorder" type="hidden" id="updateorder" value="updateorder">
                            <input type="hidden" name="dcid" id="dcid" value="<?php echo $row_categories['dcid']; ?>">
                            <input type="hidden" name="dcparent" id="dcparent" value="<?php echo $row_categories['dcparent']; ?>">
                            <input name="dcorder" type="text" id="dcorder" size="3" maxlength="3" value="<?php echo $row_categories['dcorder']; ?>">
                            <input name="submit2" type="image" src="/images/refresh.png" alt="Refresh Item" align="middle" width="16" height="16">
                          </div>
                          </label>
                                                                <div align="center"></div>
                        </form>
    Then I use this update statement to update the database.

    PHP Code:
    if(isset($_POST['updateorder'])) {
    $dcorder mysql_real_escape_string($_POST['dcorder']);
    $dcid mysql_real_escape_string($_POST['dcid']);
    $updateorder mysql_query("UPDATE documentcategories SET dcorder='$dcorder' WHERE dcid='$dcid'") or die(mysql_error());

    I would like to be able to encompass the entire table and update them with one button when submitted instead of having to update one box, then submit and the next box then submit.

    Any help would be greatly appreciated!!!

  • #2
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    You can use form field arrays.

    eg.
    you name your fields
    name = "data[<?=$row_categories['dcid']?>][nameoffield]"

    then when you submit the form you'll get data like

    Code:
    data => array(
                     1 => array( 
                              dcparent => dcparent_value
                              dcorder => dcorder_value
                           )
                      2=> array( 
                              dcparent => dcparent_value
                              dcorder => dcorder_value
                           )
    )
    Then you would do something like this :

    PHP Code:
    if(isset($_POST['updateorder'])) {
      foreach(
    $_POST['data'] as $id => $array)
      {
      
           
    $dcorder mysql_real_escape_string($array['dcorder']);
           
    $id mysql_real_escape_string($id);
           
    $updateorder mysql_query("UPDATE documentcategories SET dcorder='$dcorder' WHERE dcid='$id'") or die(mysql_error());
      }



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •