padams Posted September 19, 2007 Share Posted September 19, 2007 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); Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted September 19, 2007 Share Posted September 19, 2007 hmmmmm this is another one where your html form can really help can you give us a snippet of the previous form so we can see where/how the posted values are generated. Quote Link to comment Share on other sites More sharing options...
mgs019 Posted September 19, 2007 Share Posted September 19, 2007 Try something like: $PTries = "UPDATE playerTries SET playerTries = playerTries +1 WHERE playerID IN ($tryscorers) mysql_query($PTries) Quote Link to comment Share on other sites More sharing options...
padams Posted September 20, 2007 Author Share Posted September 20, 2007 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 Quote Link to comment Share on other sites More sharing options...
padams Posted September 21, 2007 Author Share Posted September 21, 2007 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()); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.