tags:

views:

42

answers:

2

Hello all. I have spent quite some time making a function and the last 15-20 minutes trying to figure this out. I need help!

I am selecting multiple rows from the database and then running them in a while loop.

They are available on a dropdown menu.

<form method="POST" action="adminprocess.php">
  <fieldset>
    <p>
      <label class="left2">League:</label>
      <select name="league" class="combo">
      <?php 
        $q = $database->selectAllLeagues();
        while($row=mysql_fetch_assoc($q))
        {
          $theid = $row['id'];
          extract($row);
          ?>
          <option value="<? echo $theid; ?>"> 
            <? echo $format.'_'.$game.'_'.$name.'_Season '.$season;?> 
          </option>
          <?
        }
      ?>
      </select>
    </p>
    <p>
      <input type="hidden" name="replaceleague" />
      <input type="hidden" name="format" value="<? echo $format; ?>" />
      <input type="hidden" name="game" value="<? echo $game; ?>" />
      <input type="hidden" name="name" value="<? echo $name; ?>" />
      <input type="hidden" name="season" value="<? echo $season; ?>" />
      <input type='submit' class="button" value='Select league' />
    </p>
  </fieldset>
</form>

$theid seems to be working fine dependning on which row i select on the dropdown menu. However, I cant get the values below in the hidden inputs to pass through the correct values from the row selected in the dropdown box.

It seems to always pass through the 4 variables from the first row of the database.

So basically, I need it to select the right row and use that data. What am i doing wrong!!! Thanks for reading!

+2  A: 

To directly answer your question about the while loop, it's because the hidden inputs are echoed outside the loop, after which data the last-iterated row from your database is used by PHP to output to those hidden inputs.

But I suggest that instead of using hidden form elements like that, you submit your form with the <option> with the value a user chooses, read the value (as in $_POST['league']), and fetch the row from your database with that ID and use it accordingly. (You may wish to keep the replaceleague hidden input if your application needs it of course.)

It's much easier, plus it ensures the information about the row a user chooses is coming from your database and not tampered with. In fact, for most applications this is the right way to go.

BoltClock
+2  A: 
  1. Your hidden fields are initialized outside the loop, so they'll use the values that were left over from the last iteration of the while loop. (i.e. the last fetched row)

  2. Why do you actually need the hidden fields in the first place? When you submit the form, the league field will contain the ID of the row selected in the drop-down box. Using the ID, you can fetch the other fields from the database when processing the form.

casablanca