Jump to content

[SOLVED] Checkboxes


yzerman

Recommended Posts

ok, I messed this up somewhere.

 

Here is the code in action:

http://www.sandynpaul.com/cal/bancompare2.php

 

Here is what I want to do. When 1 of the check boxes is checked, I want that(those) record(s) in the database to update.

 

The problem is, when I use $row[0] in the query, it selects random records to update (at least it looks random to me)

 

Here is the code for the script:

 

<?PHP
//mysql info
$dbuser = "me";
$dbpass = "mypass";
$dbname = "mydatabase";
mysql_connect(localhost,$dbuser,$dbpass);
@mysql_select_db($dbname) or die("Unable to select database");

//here is the query
$query = "SELECT `GUID`.`BannedGUID`, `GUID`.`banned` FROM GUID, GUID2 WHERE ( `GUID2`.`CALGUID` = `GUID`.`BannedGUID` )";

$result = mysql_query($query);

if ($result) {
echo '<table><tr><td><b><u>GUID</u></b></td><td><b><u>Profile</u></b></td><td><b><u>CAL Account</u></b></td><td><b><u>Ban Reason</u></b></td><td><b><u>CAL Banned</u></b></td></tr>';
while ($row = mysql_fetch_array($result)) {
	if ($row[1] == '1') {
		$banned = '<font color="red">YES</font>';
	} elseif ($row[1] == '0') {
		$banned = "<form action=\"$_SERVER[php_SELF]\" method=\"post\"><input type=\"checkbox\" name=\"ban\" />";
	} 
	echo '<tr><td>', $row[0], '</td><td><a href="http://caleague.com/?page=history&type=roster&uniqueid=', $row[0], '" target="_blank">Profile</a></td><td><a href="https://backoffice.caleague.com/?page=usearch&method=3&by=uniqueid&for=', $row[0],'" target="_blank">Linked Accounts</a> (Backoffice)<td><a href="http://pbbans.fbi-clan.org/index.php?type=guid&find=', $row[0], '" target="_blank">Ban Reason</a></td><td>', $banned, '</td></tr>';
	if (isset($_POST['submit'])) {
			if ($ban == "on") {
			$query2 = "UPDATE GUID SET banned = 1 WHERE BannedGUID = $row[0]";
			$result2 = mysql_query($query2);
		}
	}
}
echo '</table><br /><input name="submit" type="submit" value="Update" />';
} else {
echo 'An error has occured, please try again';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/36884-solved-checkboxes/
Share on other sites

the data currently looks like this:

 

BannedGUID | banned

------------|--------

12345        |    0

67890        |    1

23456        |    0

 

if banned = 0, and he is banned, I want the user to be able to check the checkbox for 12345 hit update, and update that record, without updating 23456

Link to comment
https://forums.phpfreaks.com/topic/36884-solved-checkboxes/#findComment-175974
Share on other sites

Ok, then perhaps instead of creating an array with the checkboxes you may consider having each one as a separate form field. Example:

 

<input type="checkbox" name="banned1" value="1">

<input type="checkbox" name="banned2" value="1">

<input type="checkbox" name="banned3" value="1">

 

This way you can set a separate variable for each field and whichever one is selected would be inserted into the database.

Link to comment
https://forums.phpfreaks.com/topic/36884-solved-checkboxes/#findComment-175980
Share on other sites

Finally got it to work, here is the working code.

<?PHP
//mysql info
$dbuser = "me";
$dbpass = "mypass";
$dbname = "mydatabase";
mysql_connect(localhost,$dbuser,$dbpass);
@mysql_select_db($dbname) or die("Unable to select database");

//form submittal
if (isset($_POST['submit'])) {
			foreach ($Selected as $s) {
				$query2 = "UPDATE GUID SET banned = 1 WHERE BannedGUID = '$s'";
				$result2 = mysql_query($query2);
				if (!$result2) {
  						 die('Invalid query: ' . mysql_error());
				}
			}
}
//here is the query
$query = "SELECT `GUID`.`BannedGUID`, `GUID`.`banned` FROM GUID, GUID2 WHERE ( `GUID2`.`CALGUID` = `GUID`.`BannedGUID` )";

$result = mysql_query($query);

if ($result) {
echo '<table><tr><td><b><u>GUID</u></b></td><td><b><u>Profile</u></b></td><td><b><u>CAL Account</u></b></td><td><b><u>Ban Reason</u></b></td><td><b><u>CAL Banned</u></b></td></tr>';
while ($row = mysql_fetch_array($result)) {
	if ($row[1] == '1') {
		$banned = '<font color="red">YES</font>';
	} elseif ($row[1] == '0') {
		$banned = "<form action=\"$_SERVER[php_SELF]\" method=\"post\"><input type=\"checkbox\" name=\"Selected[]\" value=\"$row[0]\" />";
	} 
	echo '<tr><td>', $row[0], '</td><td><a href="http://caleague.com/?page=history&type=roster&uniqueid=', $row[0], '" target="_blank">Profile</a></td><td><a href="https://backoffice.caleague.com/?page=usearch&method=3&by=uniqueid&for=', $row[0],'" target="_blank">Linked Accounts</a> (Backoffice)<td><a href="http://pbbans.fbi-clan.org/index.php?type=guid&find=', $row[0], '" target="_blank">Ban Reason</a></td><td>', $banned, '</td></tr>';
	}
	echo '</table><br /><input name="submit" type="submit" value="Update" />';
} else {
echo 'An error has occured, please try again';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/36884-solved-checkboxes/#findComment-176279
Share on other sites

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.