[PHP] Checkboxes, php and mysql

Status
Not open for further replies.
So, I'm writing a script in php to query my datbase for my forums, here is the code I have so far:

Code:
while ( $row = mysql_fetch_array($result) ) {
	//display usernames, and email address (link)
	echo("<tr>\n<td>" . $row["username"] . "</td><td> <a href=\"mailto:" . $row["user_email"] . "\">" . $row["user_email"] . "</a></td>\n</tr>");
	}
	
	//Row for submit button
	print("<input type=\"submit\" value=\"Submit\"></form>");
	//close the table
	print("</table>");

Now, if you can see it, It prints out a table with the the usernames and email addresses (plus mailto link), what I want to do is insert a checkbox in a column right before the username column.

What I need to do is be able to select the names I want to verify with checkboxes and MODIFY (chenge the 0 to 1) the value in the databases for only the names that I checked off, (if you can see, I have the $PHP_SELF and form tags filled in already so I can see what I need to do).

Does anyone understand what I'm trying to say here? It's pretty hard for me to explain wha tI want to do in PHP. If anything, i just need some info on how PHP handles checkboxes.
 
If you take a regular checkbox code such as....<input type=checkbox name="checker">, submit that, and view $checker in PHP once you've checked the box, the value for $checker is "on". I usually put value="1" in the checkbox, so that when it's checked, it's value is 1. A bit easier to use than "on".

I'm not sure I get exactly what you want to do, but you could try something like this...

<input type=checkbox name="modnames[]" value="<?= $row['username'] ?>">

Then in your PHP, go through the $modnames array, and modify those names that are in the array.

Edit: Stupid HTML.
 
Ok, let me try to restate it.

So far, I can query the database and in the field "user_active" display all usernames with a value of 0 in the database. Eacy row has an auto incrementing ID.

What I want to do, is be able to check a checkbox for the usernames that come up from the database and set the value of 0 to 1 in the database for the users that I check the box (using a Submit button and html form checkboxes).

Basically, I can't really find good examples of how php works with HTML form checkboxes.
 

mattk

Ars Scholae Palatinae
807
If I'm understanding you right, you want to modify the database to reflect the current status of the user (active or non) through a boolean. If this is the case, you could try making your checkboxes an associative array based on username.

Something like: <input type="checkbox" name="isActive[$username]" value="1"> will only return the checkboxes that are checked, and you can construct your query based on each element's key in the isActive array.
 
Thanks for the help guys, but the suggestions above were not what I wa slooking for

Ok, check it out, here's the code, hopefully it will help you guys understand, and it's my fault because I suck at explaining these things

Code:
// Request all usernames where the active flag is set to 0 and exclude the anonymous account
// Thanks to Ratchetcat and Twinze from the Ars DC hub for the <> tip
$query = "SELECT * FROM phpbb_users WHERE user_active=0 AND user_id <> '-1'" ;
// error check for database query
$result = @mysql_query($query);
if (!$result) { 
	echo("<p>Error performing query: " . mysql_error() . "</p>"); 
	exit();
}
	
//table header
print("<table border=\"1\" cellpadding=\"4\">\n<tr>\n<td> </td>\n<td><b>Username</b></td><td><b>E-Mail Address</b></td>\n</tr>\n");

//begin form tag
echo("<form action=\"" . $PHP_SELF . "\" method=\"get\" name=\"submit\">");

while ( $row = mysql_fetch_array($result) ) {
	//display usernames, and email address (link)
	echo("<tr>\n<td><input type=\"checkbox\" NAME=\"UID_ARRAY[]\" value=\"" . $row[user_id] . "\"><td>" . $row["username"] . "</td><td> <a href=\"mailto:" . $row["user_email"] . "\">" . $row["user_email"] . "</a></td>\n</tr>");
}

//Row for submit button
print("<tr>\n<td colspan=\"3\"><input type=\"submit\" value=\"verify\" action=\"post\"></form></td>\n</tr>\n");
//close the table
print("</table>\n");

//process the checkboxes when checked
if ($UID_ARRAY)
{
	$UID = implode($UID_ARRAY, ",");
	
	$result = mysql_query ("UPDATE phpbb_users SET user_active=1 WHERE user_id='$UID'");
	if(!$result) 
	{
	   echo "<B>UPDATE unsuccessful:</b> ", mysql_error();
	   exit;
	} else {
		print("All Done, <a href=\"" . $PHP_SELF . "\">check for more unverified users</a>");
	}
}
?>

now, I'm having a problem, it shows the rows, and checkboxes next to the user names in the printed out table. But here's whats wrong, when I check multiple boxes that come up, it only updates one record at a time when i click the "verify" button. Can anyone help me make this so it will do all of the checkboxes that I check off and none of the others?
 

mattk

Ars Scholae Palatinae
807
The implode function returns a string containing each element of an array concatenated by a string you define.

For example, take the following code:

$pets=array("fish","cat","dog");
echo implode($pets," and ");

You can pretty easily see what implode does. The problem with your code was that your query consisted of "...WHERE user_id='id1,id2,id3'", which doesn't work. Instead, you want to update where the user_id='id1' OR user_id='id2', etc - so I modified your implode statement to reflect that. You can echo the both queries to see the difference.
 
Status
Not open for further replies.