#1
  1. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    277
    Rep Power
    0

    A Do While and an Update Command


    I've set php to use a do-while command to repeat the number of rows for a form dependent on a value that I set. Eventually the user will be able to enter a number for the form to give as many row in the form as the user enters. For example, the user wants to update 25 items, so they enter 25 and the page loads 25 rows of the form.

    Anyway, so I have the thing dynamically creating the rows along with the corresponding fields, but when I run the update, it doesnt do anything. My question is, how does mysql know that there should be 25 different update commands? Have I missplaced a command somewhere here? The way it looks now, since the variables on each row are the same name, I'm not sure how it would be able to know which values go on with row.

    PHP Code:
          <form action="<?php echo $editFormAction?>" method="POST" name="frmUpdate" id="frmUpdate">
            <table border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td><div align="center">
                  <table width="100%" border="0" cellpadding="0" cellspacing="0">
                    <tr bgcolor="#CCCCCC">
                      <td width="100"><div align="center"><strong>Asset Number</strong></div></td>
                      <td width="100"><div align="center"><strong>Location</strong></div></td>
                      <td width="100"><div align="center"><strong>Department</strong></div></td>
                      <td width="100"><div align="center"><strong>Room</strong></div></td>
                      <td width="100"><div align="center"><strong>Status</strong></div></td>
                    </tr>
                  </table>
                </div></td>
              </tr>
              <tr>
                <td><table width="100%">
                  <?php
          $i 
    0;
          do{
    ?>
                  <tr>
                    <td width="100"><div align="center">
                        <input name="txtAsset" type="text" id="txtAsset">
                    </div></td>
                    <td width="100"><div align="center">
                        <select name="cmbLocation" id="cmbLocation">
                          <?php
    do {  
    ?>
                          <option value="<?php echo $row_rsLocation['locationID']?>"><?php echo $row_rsLocation['locationDesc']?></option>
                          <?php
    } while ($row_rsLocation mysql_fetch_assoc($rsLocation));
      
    $rows mysql_num_rows($rsLocation);
      if(
    $rows 0) {
          
    mysql_data_seek($rsLocation0);
          
    $row_rsLocation mysql_fetch_assoc($rsLocation);
      }
    ?>
                        </select>
                    </div></td>
                    <td width="100"><div align="center">
                        <select name="cmbDepartment" id="cmbDepartment">
                          <?php
    do {  
    ?>
                          <option value="<?php echo $row_rsDepartment['departmentID']?>"><?php echo $row_rsDepartment['departmentDesc']?></option>
                          <?php
    } while ($row_rsDepartment mysql_fetch_assoc($rsDepartment));
      
    $rows mysql_num_rows($rsDepartment);
      if(
    $rows 0) {
          
    mysql_data_seek($rsDepartment0);
          
    $row_rsDepartment mysql_fetch_assoc($rsDepartment);
      }
    ?>
                        </select>
                    </div></td>
                    <td width="100"><div align="center">
                        <input name="txtRoom" type="text" id="txtRoom">
                    </div></td>
                    <td width="100"><div align="center">
                        <select name="cmbStatus" id="cmbStatus">
                          <?php
    do {  
    ?>
                          <option value="<?php echo $row_rsStatus['statusID']?>"><?php echo $row_rsStatus['statusDesc']?></option>
                          <?php
    } while ($row_rsStatus mysql_fetch_assoc($rsStatus));
      
    $rows mysql_num_rows($rsStatus);
      if(
    $rows 0) {
          
    mysql_data_seek($rsStatus0);
          
    $row_rsStatus mysql_fetch_assoc($rsStatus);
      }
    ?>
                        </select>
                    </div></td>
                  </tr>
                  <?php
                $i
    ++;
            }
          while(
    $i<10);
          
    ?>
                </table></td>
              </tr>
              <tr>
                <td><div align="right">
                  <input name="btnSubmit" type="submit" id="btnSubmit" value="Submit">
                </div></td>
              </tr>
            </table>
            <input type="hidden" name="MM_update" value="frmUpdate">
          </form>
  2. #2
  3. Hockey face
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    St. Catharines, Canada
    Posts
    8,150
    Rep Power
    1317
    mysql doesn't do an update because you have no UPDATE statement anywhere in that code.
  4. #3
  5. Anemic Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,791
    Rep Power
    9432
    If you have 25 copies of a certain field (like txtAsset) each one needs to have a unique value so that PHP can identify which field is i=0 and which is i=24. The easiest way to do this is using an array:
    Code:
    <input name="txtAsset[$i]" type="text" id="txtAsset$i">
    <select name="cmbLocation[$i]" id="cmbLocation$i">
    etc.
    Notice how they each have a $i in their id too, otherwise you'd have multiple HTML elements with the same ID and that's not allowed.

    Then when you get the fields using $_POST["txtAsset"], it will be an array instead of a regular string value. You loop from j=0 to 24 and execute your query using $_POST["txtAsset"][$j], $_POST["cmbLocation"][$j] and so on.

    Make sure you specify in your form how many fields there are. Or instead of a for(j=0;j<i;j++) you can use a foreach($_POST["txtAsset"] as $key=>$asset) and use the $key to access the other form fields via $_POST["cmbLocation"][$key].
  6. #4
  7. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    277
    Rep Power
    0
    Again, guelph provides no help and gives an answer that is stupid. Of course I have an update in my code, it's just not pasted here. It has nothing to do with the problem.

    Requinix, that's what I was looking for. Thanks for the help. Things are pretty crazy right now, but I'l try redesigning the code like that and see what I can do. Thanks again!

IMN logo majestic logo threadwatch logo seochat tools logo