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
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

Link to comment
Share on other sites

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());

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.