Jump to content

[SOLVED] Updating info from a simple form...


galvin

Recommended Posts

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. 

Link to comment
https://forums.phpfreaks.com/topic/134823-solved-updating-info-from-a-simple-form/
Share on other sites

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?

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

 

 

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.