• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 653
  • Last Modified:

Submit updates to a mysql table through checkboxes

I am having a little trouble. what i am trying to do is retrieve a list of unauthorized users, and list them (no problem, got that part taken care of).

Where i am running into trouble is after listing the unauthorized users in a table, i want the admin to be able to click a checkbox next to each of the user he wants to authorize and then update the users authorization status.

How would i go about implementing this?
<?php
	  if($_SESSION['permissions'] == "2")
	{
	  
$con = mysql_connect($databse, $user, $dbpass);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db($table, $con);
 
$result = mysql_query("SELECT * FROM Users WHERE authorized = '0'");
 
echo "<table width='200' bgcolor='000000' border='1'>
<tr>
<th width='25%'>Username</th>
<th width='7%'>Authorize</th>
</tr>";
 
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['username'] . "</td>";
  echo "<td>" 
  ?>
  
  <form name="authorize" action="authorize.php" method="post">
  
  <input type="radio" name="authorize" />
 
  
  
  
  <?php
  "</td>";
  echo "</tr>";
  }
echo "</table>";
 
mysql_close($con);
}
?>
		
	<input type="submit" name="Submit" value="Authorize" />
	</form>

Open in new window

0
Catcherman16
Asked:
Catcherman16
2 Solutions
 
quincydudeCommented:
The idea is like putting a checkbox (all with the same name) next to each username

 <input type="checkbox" name="userlist[]" value="$userid"> , where $userid is the user of each username.

After submit the form , you can get the selected list by



<?php
foreach($_POST['userlist'] as &$value) {
    echo $value;
}

Open in new window

0
 
Ray PaseurCommented:
Radio buttons are mutually exclusive - even if you have 5 of them, you get no more than one in the $_POST input.  You can get zero, if the client selected nothing.

Checkboxes are not mutually exclusive - You can get zero or more (up to the total number of checkboxes) depending on what the client selected.  A checkbox that is not checked does NOT come through in the $_POST input.  Why is this important?  

If you put up a form with a checkbox that is checked (ie: a member in good standing) and the client un-checks it, the checkbox will be absent in $_POST - you can't check for a value of "off" or something helpful like that.  Instead you have to know that the checkbox exists and know that its absence in $_POST means a change of state.  A nuisance, but forewarned is forearmed!

I think when you are testing with these things it is helpful to put this into the script: var_dump($_POST);

best of luck with your project, ~Ray
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now