Jump to content

[SOLVED] Dynamically updating database


padams

Recommended Posts

I have drop-down menus created on a previous page, created dynamically so the number of them varies. When the form is submitted I need a script that will automatically add 1 to the value in the playerTries column in the players table, for every drop-down menu. I've tried the code below but don't fully understand what's happening, and why it's not working.

 

$tryscorers = implode(", ", $_POST['try']);

 

$trycounter =1;

foreach ($_POST['try'] as $trykey => $tryval){

$tryqry = "UPDATE players SET playerTries = playerTries +1 WHERE playerID IN ($tryscorers)";

}

$tryqry = substr($tryqry,0,-1);

echo $tryqry;

$tryqry = mysql_query($tryqry);

Link to comment
https://forums.phpfreaks.com/topic/69865-solved-dynamically-updating-database/
Share on other sites

Unfortunately the code:

$PTries = "UPDATE playerTries SET playerTries = playerTries +1 WHERE playerID IN ($tryscorers)";

mysql_query($PTries);

didn't work. When I echo $tryscorers it shows the correct playerIDs, so that is being set up okay. I just can't work out how to iterate through the array, updating the appropriate player record for each element in the array.

 

The code that creates the drop-downs on the previous page is:

 

<?php

  $count = 1;

  do { ?>

  Try <?php echo $count; ?>:

  <select name="try[<?php echo $count; ?>]">

    <?php do {

?>

      <option value="<?php echo $rstrylists['playerID']; ?>"><?php echo $rstrylists['playerFirstName']; ?> <?php echo $rstrylists['playerLastName']; ?></option>

      <?php } while ($rstrylists = mysql_fetch_assoc($trylists_query)) ?>

    </select>

  <br>

<?php

$count++;

mysql_data_seek($trylists_query, 0);

} while ($count <= $trycount) ?>          <----- $trycount is the number of tries scored

Thanks for the help. I took what you suggested and incorporated into what I know about looping through arrays, and it seems to have worked.

 

foreach ($_POST['try'] as $trykey => $tryval){

$PTries = "UPDATE players SET playerTries = playerTries +1 WHERE playerID IN ($tryval)";

$PTries_query = mysql_query($PTries) or die(mysql_error());

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.