Jump to content

Checkbox not changing


bacarudaguy

Recommended Posts

I've got my entire script on this page just about running, but having an issue with the checkbox not changing. Can anyone advise on where I might have skipped something?

 

<?php
//require config file
require_once ('./includes/config.inc.php');

//set page title (optional for index.php)
$page_title = 'Edit User';

//include header
include ('./includes/header.html');

// Check for a valid user ID, through GET or POST.
if ((isset($_GET['user_id'])) && (is_numeric($_GET['user_id'])) ) { // Accessed through manage_users.php
$id = $_GET['user_id'];
} elseif ((isset($_POST['user_id'])) && (is_numeric($_POST['user_id'])) ) { // Form has been submitted.
$id = $_POST['user_id'];
} else { // No valid ID, kill the script.
echo '<p class="error">You\'ve accessed this page in error. Please try again.';
include ('./includes/footer.html');
exit();
}

//require database connection to check for admin
require_once ('./connect.php');

//error checking for form
if (isset($_POST['submitted'])) { 

//check for first name
if (eregi ('^[[:alpha:]\.\' \-]{2,15}$', stripslashes(trim($_POST['first_name'])))) {
	$fn = escape_data($_POST['first_name']);
} else {
	$fn = FALSE;
	echo '<p class="error">Please enter your first name!</p>';
}

//check last name
if (eregi ('^[[:alpha:]\.\' \-]{2,15}$', stripslashes(trim($_POST['last_name'])))) {
	$ln = escape_data($_POST['last_name']);
} else {
	$ln = FALSE;
	echo '<p class="error">Please enter your last name!</p>';
}

//check screen name
if (eregi ('^[[:alpha:]\.\' \-]{2,15}$', stripslashes(trim($_POST['screen_name'])))) {
	$sn = escape_data($_POST['screen_name']);
} else {
	$sn = FALSE;
	echo '<p class="error">Please enter your screen name!</p>';
}

//check email
if (eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['email'])))) {
	$e = escape_data($_POST['email']);
} else {
	$e= FALSE;
	echo '<p class="error">Please enter a valid email address!</p>';
}

//set admin variable
$a = $_POST['admin'];

//check all variables
if ($fn && $ln && $sn && $e) {

	//check if admin is true or false and set accordingly
	if ($a == '1') {

		$query = "UPDATE users SET first_name='$fn', last_name='$ln', screen_name='$sn', email='$e', admin='1' WHERE user_id='$id'";
		$result = mysql_query ($query) or trigger_error ("Query: $query\n<br />MySQL Error: " . mysql_error());

		if (mysql_affected_rows() == 1) { // If it ran OK.
			echo '<p><b>User updated successfully and ADDED as admin!</b></p>';
			include ('./includes/footer.html');
			exit();

		} else {

			echo '<p class="error">The user could NOT be updated due to a system error. We apologize for any inconvenience.</p>';

		} //end if of mysql_affected_rows

	} else { //if admin is false

		$query = "UPDATE users SET first_name='$fn', last_name='$ln', screen_name='$sn', email='$e', admin='0' WHERE user_id='$id'";
		$result = mysql_query ($query) or trigger_error ("Query: $query\n<br />MySQL Error: " . mysql_error());

		if (mysql_affected_rows() == 1) { // If it ran OK.
			echo '<p><b>User updated successfully, but NOT added as an admin!</b></p>';
			include ('./includes/footer.html');
			exit();

		} else {

			echo '<p class="error">The user could NOT be updated due to a system error. We apologize for any inconvenience.</p>';

		} //end if of mysql_affected_rows

	} //end if admin check

} else {

	//something else failed
	echo '<p class="error">Please try again.</p>';

} //end check all variables

} //end of main IF

//pull user info to edit
$query = "SELECT first_name, last_name, screen_name, email, admin FROM users WHERE user_id=$id";
$result = mysql_query ($query) or trigger_error ("Query: $query\n<br />MySQL Error: " . mysql_error());

//valid user id, let's edit
if (mysql_num_rows($result) == 1) {

//pull user info
$row = mysql_fetch_array($result, MYSQL_NUM);

//create edit form
echo '<form action="edit_user.php" method="post">
<fieldset>

<legend>Edit Tournament Director</legend>

<p><b>First Name:</b> <input type="text" name="first_name" size="15" maxlength="15" value="' . $row[0] . '" /></p>

<p><b>Last Name:</b> <input type="text" name="last_name" size="15" maxlength="30" value="' . $row[1] . '" /></p>

<p><b>Screen Name:</b> <input type="text" name="screen_name" size="30" maxlength="30" value="' . $row[2] . '" /> </p>

<p><b>Email Address:</b> <input type="text" name="email" size="40" maxlength="40" value="' . $row[3] . '" /> </p>

<p><b>Admin:</b>';

//determine admin status
if ($row[4] === '1') {
	echo '<input type="checkbox" checked="checked" name="admin" value="' . $row[4] . '" />';
} elseif ($row[4] === '0') {
	echo '<input type="checkbox" name="admin" value="' . $row[4] . '" />';
} else {
	echo '';
}

echo '</p>

<p><input type="submit" name="submit" value="Submit" /> <input type="button" name="cancel" value="Cancel" onClick="location.href=(\'manage_users.php\');" /></p>

<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="user_id" value="' . $id . '" />

</fieldset>

</form>';

} else { //not a valid user id

echo '<p class="error">You\'ve accessed this page in error. Please try again.';

} //end of mysql_affected_row

include ('./includes/footer.html');
?>

Link to comment
https://forums.phpfreaks.com/topic/184609-checkbox-not-changing/
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.