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