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

Link to comment
Share on other sites

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

 

 

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.