www.webdeveloper.com
Results 1 to 3 of 3

Thread: Problem in Loop with Multiple Update

  1. #1
    Join Date
    Feb 2008
    Location
    Boondocks, Ohio
    Posts
    127

    Problem in Loop with Multiple Update

    I'm trying to provide a much faster way of verifing information, therefore I would like to do a multiple update rather than having to edit each record one at a time.

    Which leads me to my test:

    PHP Code:
    <?php
    $sql
    ="SELECT * FROM FootballCamp LIMIT 10";
    $result=mysql_query($sql);

    // Count table rows
    $count=mysql_num_rows($result);
    ?>
    <title>Multiple Update Test</title>

    <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>Waiver Signed?</strong></td>
              </tr>
    <?php while($rows=mysql_fetch_array($result)){ ?>
              <tr>
                <td align="center"><? $id[]=$rows['ID']; ?>
                  <? echo $rows['ID']; ?></td>
                <td align="center"><input type="checkbox" id="WaiverSigned[]" name="WaiverSigned[]" value="1" /></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
    extract($_POST);
    if(
    $Submit){
    for(
    $i=0;$i<$count;$i++){
    $sql1="UPDATE FootballCamp SET WaiverSigned='$WaiverSigned[$i]' WHERE ID='$id[$i]'";
    //$result1=mysql_query($sql1);
    echo $sql1 '<br>';
    }
    }
    mysql_close();
    ?>
    For testing purposes I don't actually update the database, so to check my data I echo the sql statements that it creates.

    And let's say I check 2 boxes (doesn't matter which ones) it will only update the first two records with 1 and the rest will be 0 even if I didn't choose them.

    And it would output this:

    UPDATE FootballCamp SET WaiverSigned='1' WHERE ID='19'
    UPDATE FootballCamp SET WaiverSigned='1' WHERE ID='18'
    UPDATE FootballCamp SET WaiverSigned='' WHERE ID='17'
    UPDATE FootballCamp SET WaiverSigned='' WHERE ID='16'
    UPDATE FootballCamp SET WaiverSigned='' WHERE ID='15'
    UPDATE FootballCamp SET WaiverSigned='' WHERE ID='14'
    UPDATE FootballCamp SET WaiverSigned='' WHERE ID='13'
    UPDATE FootballCamp SET WaiverSigned='' WHERE ID='12'
    UPDATE FootballCamp SET WaiverSigned='' WHERE ID='11'
    UPDATE FootballCamp SET WaiverSigned='' WHERE ID='10'


    Any ideas???

    Thanks
    Last edited by cinematic_jesi; 02-27-2009 at 11:08 AM.

  2. #2
    Join Date
    Feb 2008
    Location
    Boondocks, Ohio
    Posts
    127
    Okay so I switched it with textfields and it worked fine if I manually inputted 0 or 1. So its something wrong with my checkboxes.. ..

  3. #3
    Join Date
    Feb 2008
    Location
    Boondocks, Ohio
    Posts
    127
    bump?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles