galvin Posted November 30, 2008 Share Posted November 30, 2008 I have a simple form... <form action="updatewinners.php" method="post"> <table id="login"> <tr> <td><input type="checkbox" name="winner" value="1" />Eagles</td> </tr> <tr> <td><input type="checkbox" name="winner" value="2" />Cowboys</td> </tr> <tr> <td><input type="checkbox" name="winner" value="3" />Giants</td> </tr> <tr> <td><input type="checkbox" name="winner" value="4" />Redskins</td> </tr> <td><input type="submit" name="submit" value="Update Winners" class="submit" /></td> </tr> </table> </form> ....and on the updatewinners.php page (code below), I am trying to process the information submitted via the form such that for any teams that are checked (whether it's 1, 2, 3 or all 4 of them), it will check my "Picks" table in MySQL and if those teamids are in the table with a result of 'o', it will change the result to 'w'. Here is the updatewinners.php code that I thought would work, but it's not working.... if (isset($_POST['submit']) && $_POST['submit'] == "Update Winners" ) { /* Not sure I need an array here but my thinking was to put all the "values" from the form into an array */ $allteams = array(1,2,3,4); if (in_array($_POST['value'],$allteams)) { $query = "UPDATE picks SET result = 'w' WHERE teamid = '{$_POST["value"]}' AND result = 'o'"; $updateWinners = mysql_query($query, $connection); if (!$updateWinners) { die("Database query failed: " . mysql_error()); } else { $success = "The Winners were updated!"; } } else { $didnotwork = "No teams are in table with a result of 'o'"; } } else { $didnotwork = "Info was not submitted. Try again."; } I'm obviously missing some step with correctly processing the $_POST['value'] info (I think), so if anyone could help me, I would be very grateful. Quote Link to comment Share on other sites More sharing options...
KrisNz Posted November 30, 2008 Share Posted November 30, 2008 your checkboxes are called "winner" not "value", ie use $_POST['winner']. doing a print_r($_POST); might make things a little clearer for you as you'll see how the post array is structured. Quote Link to comment Share on other sites More sharing options...
galvin Posted November 30, 2008 Author Share Posted November 30, 2008 Thanks. I did that and I'm closer now. But if I check off only "Eagles", the array when "printed" shows as Array ( [0] => Array ( [0] => 1 ) ) How do I call to all the values (in this case, it's "1"). I tried '$winteams[0]' but that just brings back "Array" (when I need "1"). Assuming that makes sense, how do I do that? Quote Link to comment Share on other sites More sharing options...
KrisNz Posted November 30, 2008 Share Posted November 30, 2008 ahh ok, first of all, change your checkbox field names from winner to winners[] - this will allow multiple values to be posted back (i.e an array). You then need to loop through each value that was posted back and update it e.g foreach ($_POST['winners'] as $key => $teamId) { mysql_query("update picks set result = 'w' WHERE winner = '$teamId' AND result = 'o' ") or die(mysql_error()); } Quote Link to comment Share on other sites More sharing options...
galvin Posted November 30, 2008 Author Share Posted November 30, 2008 Thank you sir! 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.