Location: PHPKode > projects > Run > Run-0.8.0.2/Run-0.8.0.2/editrunner.php
<?php
include("system/environment.php");
include("system/functions.php");
?>

<html>
<?php
include("system/head.php");
?>

<script language="javascript" type="text/javascript">
function confirmDelete() {
	var agree = confirm("Are you sure you want to remove this runner and all their data?");
	if(agree)
		return true;
	else
		return false;
}
</script>

<body>

<div class="logo"><img src="system/imgs/runsmall.png"></div>

<?php
//get the data for the selected ID and put it into the array $edit
$editquery = mysql_query("select * from $mysqlTableRunners where ID='".$_GET['ID']."'");
$edit = mysql_fetch_array($editquery, MYSQL_ASSOC);

if ($_POST['submit']) {
//this part deals with checking if the data entered are OK or not
	//first check if a runner with the input username already exists in the database, except if it's the existing username of the runner that is being edited
	//first define the query, then run mysql_num_rows to see if any of the rows show up
	$usernamecheckquery = mysql_query("select ID, RunnerID from $mysqlTableRunners where RunnerID='".$_POST['Username']."'");
	$usernamecheck = mysql_fetch_assoc($usernamecheckquery);
	$usernamerowscheck = mysql_num_rows($usernamecheckquery);
	//first check if returned ID equals asked for ID. if it does, set the $usernameduplicate to 0 and go on, because this is the same user updating their information
	//however, if RunnerID from the database doesn't equal the RunnerID that is being edited and there is one such entry (row), it means that a user with that username already exists so set $usernameduplicate to 1.
	//print the form out with all values except password + a warning saying that such username already exists
	if ($usernamecheck['ID'] == $_GET['ID']) {
		$usernameduplicate = 0;
	} elseif ($usernamecheck['RunnerID'] != $edit['RunnerID'] && $usernamerowscheck == 1) {
		$usernameduplicate = 1;
	} elseif ($editD['RunnerID'] == 'Administrator' || $edit['RunnerID'] == 'administrator') {
		$usernameduplicate = 1;
	}

	//next check if provided passwords are the same
	//if they are not print the whole form out with all values except passwords + a warning saying that the entered passwords do not match
	if ($_POST['Password'] != $_POST['RetypePassword']) {
		$passnomatch = 1;
	}

	//check if the date of birth entered is the correct date. since nothing else but what the form provides can be entered, this is neccessary for 28, 30, 31 day check for the selected month 
	if (checkdate($_POST['BirthMonth'], $_POST['BirthDay'], $_POST['BirthYear']) != 1) {
		$badbirthdate = 1;
	}

	//check if the member since date entered is the correct date. since nothing else but what the form provides can be entered, this is neccessary for 28, 30, 31 day check for the selected month 
	if (checkdate($_POST['MemberSinceMonth'], $_POST['MemberSinceDay'], $_POST['MemberSinceYear']) != 1) {
		$badmembersincedate = 1;
	}

	//
	if (strlen($_POST['Username']) == 0 || strlen($_POST['FirstName']) == 0 || strlen($_POST['LastName']) == 0) {
		$missing = 1;
	}


	//if either (1) username already exists or (2) passwords don't match, or an (3a) invalid date of birth has been entered, or an (3b) invalid member since date has been enetered, or if (4) username, (5) password, (6) first name or (7) last name are missing, display the form below that will show what is wrong with that data or what data is missing - along with all other input data (except for passwords)
	if ($usernameduplicate == 1 || $passnomatch == 1 || $badbirthdate == 1 || $badmembersincedate == 1 || $missing == 1) {
?>


<div class="content">
<p class="header">Edit Runner</p>
<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
<table border="0">
<tr><td class="right">Username:</td>
<td class="left"><input type="text" name="Username" size="20" maxlength="30" value="<?php echo stripslashes($_POST['Username'])?>" class="form"></td></tr>
<?php
	if ($usernameduplicate == 1) {
		echo "<tr><td colspan=\"2\" class=\"left\"><span class=\"warning\">A runner with this username already exists.</span></td></tr>";
	} elseif (strlen($_POST['Username']) == 0) {
		echo "<tr><td colspan=\"2\" class=\"left\"><span class=\"warning\">You must enter a username.</span></td></tr>";
	}
?>

<tr><td class="left" colspan="2">Leave empty to keep the current password.</td></tr>
<tr><td class="right">Password:</td>
<td class="left"><input type="password" name="Password" size="20" maxlength="40" value="<?php echo $_POST['Password']?>" class="form"></td></tr>
<tr><td class="right">Retype Password:</td>
<td class="left"><input type="password" name="RetypePassword" size="20" maxlength="40" value="<?php echo $_POST['RetypePassword']?>" class="form"></td></tr>
<?php
	if ($passnomatch == 1) {
		echo "<tr><td colspan=\"2\" class=\"left\"><span class=\"warning\">Entered passwords don't match. Retype both passwords.</span></td></tr>";
	} else {
		echo "<tr><td>&nbsp;</td></tr>";
	}
?>
<tr><td class="right">First Name:</td>
<td class="left"><input type="text" name="FirstName" size="20" maxlength="30" value="<?php echo stripslashes($_POST['FirstName'])?>" class="form"></td></tr>
<?php
	if (strlen($_POST['FirstName']) == 0) {
		echo "<tr><td colspan=\"2\" class=\"left\"><span class=\"warning\">You must enter your first name.</span></td></tr>";
	}
?>

<tr><td class="right">Last Name:</td>
<td class="left"><input type="text" name="LastName" size="20" maxlength="30" value="<?php echo stripslashes($_POST['LastName'])?>" class="form"></td></tr>
<?php
	if (strlen($_POST['LastName']) == 0) {
		echo "<tr><td colspan=\"2\" class=\"left\"><span class=\"warning\">You must enter your last name.</span></td></tr>";
	}
?>

<tr><td class="right">Date of Birth:</td>
<td class="left">
<select name="BirthDay" class="form">
<option value="<?php echo $_POST['BirthDay']?>"><?php echo strlen($_POST['BirthDay']) == 0 ? 'Day' : $_POST['BirthDay']; ?></option>
<?php
generateDays();
?>
</select>

<select name="BirthMonth" class="form">
<option value="<?php echo $_POST['BirthMonth']?>"><?php echo strlen($_POST['BirthMonth']) == 0 ? 'Month' : date("F", mktime(0, 0, 0, $_POST['BirthMonth'], 1, 0, 0)); ?></option>
<?php
generateMonths();
?>
</select>

<select name="BirthYear" class="form">
<option value="<?php echo $_POST['BirthYear']?>"><?php echo strlen($_POST['BirthYear']) == 0 ? 'Year' : $_POST['BirthYear']; ?></option>
<?php
generateYears();
?>
</select>
</td></tr>
<?php
	if ($badbirthdate == 1) {
		echo "<tr><td colspan=\"2\"><span class=\"warning\">The date you entered is not correct.</span></td></tr>";
	}
?>


<tr><td class="right">Gender:</td>
<td class="left"><input type="radio" name="Gender" value="F" <?php if ($_POST['Gender'] == "F") { echo "checked";}?> class="form">Female<br>
<input type="radio" name="Gender" value="M" <?php if ($_POST['Gender'] == "M") { echo "checked";}?> class="form">Male</td></tr>

<tr><td class="right">Member Since:</td>
<td class="left">
<select name="MemberSinceDay" class="form">
<option value="<?php echo $_POST['MemberSinceDay']?>"><?php echo $_POST['MemberSinceDay']?></option>
<?php
generateDays();
?>
</select>

<select name="MemberSinceMonth" class="form">
<option value="<?php echo $_POST['MemberSinceMonth']?>"><?php echo date("F", mktime(0, 0, 0, $_POST['MemberSinceMonth']+1, 0, 0))?></option>
<?php
generateMonths();
?>
</select>

<select name="MemberSinceYear" class="form">
<option value="<?php echo $_POST['MemberSinceYear']?>"><?php echo $_POST['MemberSinceYear']?></option>
<?php
generateYears();
?>
</select>
</td></tr>
<?php
	if ($badmembersincedate == 1) {
		echo "<tr><td colspan=\"2\"><span class=\"warning\">The date you entered is not correct.</span></td></tr>";
	}
?>

<tr><td class="right">Status:</td>
<td class="left"><input type="radio" name="Active" value="Y" <?php if ($_POST['Active'] == "Y") { echo "checked";}?> class="form">Active<br>
<input type="radio" name="Active" value="N" <?php if ($_POST['Active'] == "N") { echo "checked";}?> class="form">Inactive</td>
</tr>

<tr><td class="right">Distance Unit:</td>
<td class="left">
<select name="DistanceUnit" class="form">
<?php
distanceUnits($_POST['DistanceUnit']);
?>
</select>
</td></tr>

<tr><td class="right">Temperature Unit:</td>
<td class="left">
<select name="TemperatureUnit" class="form">
<?php
temperatureUnits($_POST['TemperatureUnit']);
?>
</select>
</td></tr>

<tr><td class="right">Weight Unit:</td>
<td class="left">
<select name="WeightUnit" class="form">
<?php
weightUnits($_POST['WeightUnit']);
?>
</select>
</td></tr>

<tr><td class="right">View Mode:</td>
<td class="left"><input type="radio" name="ViewMode" value="0" <?php if ($_POST['ViewMode'] == "0") { echo "checked"; }?> class="form">Full<br>
<input type="radio" name="ViewMode" value="1" <?php if ($_POST['ViewMode'] == "1") { echo "checked"; }?> class="form">Summary</td></tr>

<tr><td class="right">Notes:</td>
<td class="left"><textarea name="Notes" rows="4" cols="30" wrap="virtual" class="form"><?php echo stripslashes(htmlentities($_POST['Notes']))?></textarea></td></tr>


<tr><td class="right"></td>
<td class="left"><input type="submit" name="submit" value="Submit" accesskey="s" class="form">&nbsp;<input type="submit" value="Wipe" accesskey="w" class="form"></td></tr>
<tr><td class="right" colspan="2"><?php printf("<a href=\"%s?ID=%s&delete=1\" onClick=\"return confirmDelete()\" title=\"Delete Runner: Alt+D\" accesskey=\"d\" class=\"url\"><u>D</u>elete Runner</a>", $_SERVER['PHP_SELF'], $_GET['ID']); ?> | <a href="javascript:window.close();" title="Close This Window: Alt+C" accesskey="c" class="url"><u>C</u>lose</a></td></tr>


</table>
</form>
</div>


<?php
//if all entered data are OK, this part does the few final touches and finally enters everything into the database
	} else {
		//if the password was not changed leave it as it is. if it was then use sha1 to encrypt it.
		if ($_POST['Password'] == $edit['Password']) {
			$password = $edit['Password'];
		} else {
			//encrypt the password with sha1
			$password = sha1($_POST['Password']);
		}

		$memberSince = dateFormat($_POST['MemberSinceDay'], $_POST['MemberSinceMonth'], $_POST['MemberSinceYear']);

		$measurements = measurementUnits($_POST['DistanceUnit'], $_POST['TemperatureUnit'], $_POST['WeightUnit']);

		$dateOfBirth = dateFormat($_POST['BirthDay'], $_POST['BirthMonth'], $_POST['BirthYear']);

		$viewMode = $_POST['ViewMode'] . "0-11111111111";

		//this is the query used to enter the submitted data into the database
		if (strlen($_POST['Password']) == 0) {
			$query = "update $mysqlTableRunners set RunnerID='".$_POST['Username']."', FirstName='".$_POST['FirstName']."', LastName='".$_POST['LastName']."', DateOfBirth='".$dateOfBirth."', Gender='".$_POST['Gender']."', ViewMode='$viewMode', Measurements='".$measurements."', Notes='".$_POST['Notes']."', MemberSince='".$memberSince."', Active='".$_POST['Active']."' where ID='".$edit['ID']."'";
		} else {
			$query = "update $mysqlTableRunners set RunnerID='".$_POST['Username']."', Password='".$password."', FirstName='".$_POST['FirstName']."', LastName='".$_POST['LastName']."', DateOfBirth='".$dateOfBirth."', Gender='".$_POST['Gender']."', ViewMode='$viewMode', Measurements='".$measurements."', Notes='".$_POST['Notes']."', MemberSince='".$memberSince."', Active='".$_POST['Active']."' where ID='".$edit['ID']."'";
		}
		$result = mysql_query($query);

		//after data has been input into the database, show this message for a brief period of time and then close the window
		echo "<html><head><meta http-equiv=\"refresh\" content=\"3; url=javascript:window.close()\"></head><body><div class=\"inputconfirm\">You have successfully edited <b>{$edit['RunnerID']}</b>'s data.</div></body></html>";

	}

} elseif ($_GET['delete'] == 1) {
	//when Delete Runner link is clicked, after confirmation, the mysql query below is executed and the runner along with all their running logs and shoes is deleted.
	$getRunnerIDQuery = mysql_query("select RunnerID from $mysqlTableRunners where ID='".$_GET['ID']."'");
	$getRunnerID = mysql_fetch_assoc($getRunnerIDQuery) or die(mysql_error());

	echo "<div class=\"inputconfirm\">Deleting runner profile...";
	$deleteRunner = mysql_query("delete from $mysqlTableRunners where ID='".$_GET['ID']."'") or die(mysql_error());

	echo "<br>Deleting runner's logs...";
	$deleteRunningLogs = mysql_query("delete from $mysqlTableRunningLogs where RunnerID='".$getRunnerID['RunnerID']."'") or die(mysql_error());

	echo "<br>Deleting runner's shoes...";
	$deleteShoes = mysql_query("delete from $mysqltableshoes where RunnerID='".$getRunnerID['RunnerID']."'") or die (mysql_error());
	echo "<html>\n<head>\n<meta http-equiv=\"refresh\" content=\"5; url=javascript:window.close()\">\n</head>\n<body>\n<br><br>{$getRunnerID['RunnerID']} and all their date were successfully deleted.</div>\n</body>\n</html>";

} else {

//the form below is first shown when the window is opened, or in other words, before submit is clicked. it is filled with data from the database for the selected runner.
?>

<div class="content">
<p class="header">Edit Runner</p>

<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
<table border="0">

<tr><td class="right">Username:</td>
<td class="left"><input type="text" name="Username" size="20" maxlength="30" value="<?php echo stripslashes($edit['RunnerID'])?>" class="form"></td></tr>

<tr><td class="left" colspan="2">Leave empty to keep the current password.</td></tr>
<tr><td class="right">Password:</td>
<td class="left"><input type="password" name="Password" size="20" maxlength="40" value="" class="form"></td></tr>

<tr><td class="right">Retype Password:</td>
<td class="left"><input type="password" name="RetypePassword" size="20" maxlength="40" value="" class="form"></td></tr>
<tr><td>&nbsp;</td></tr>

<tr><td class="right">First Name:</td>
<td class="left"><input type="text" name="FirstName" size="20" maxlength="30" value="<?php echo stripslashes($edit['FirstName'])?>" class="form"></td></tr>

<tr><td class="right">Last Name:</td>
<td class="left"><input type="text" name="LastName" size="20" maxlength="30" value="<?php echo stripslashes($edit['LastName'])?>" class="form"></td></tr>

<?php
//reformat date date from YYYY-MM-DD into array(YYYY, MMM, DD)
$dateofbirth = explode("-", $edit['DateOfBirth']);
?>
<tr><td class="right">Date of Birth:</td>
<td class="left">
<select name="BirthDay" class="form">
<option value="<?php echo $dateofbirth['2']?>"><?php echo $dateofbirth['2']?></option>
<?php
generateDays();
?>
</select>

<select name="BirthMonth" class="form">
<option value="<?php echo $dateofbirth['1']?>"><?php echo date("F", mktime(0, 0, 0, $dateofbirth['1']+1, 0, 0))?></option>
<?php
generateMonths();
?>
</select>

<select name="BirthYear" class="form">
<option value="<?php echo $dateofbirth['0']?>"><?php echo $dateofbirth['0']?></option>
<?php
generateYears();
?>
</select>
</td></tr>

<tr><td class="right">Gender:</td>
<td class="left"><input type="radio" name="Gender" value="F" <?php if ($edit['Gender'] == "F") { echo "checked";}?> class="form">Female<br>
<input type="radio" name="Gender" value="M" <?php if ($edit['Gender'] == "M") { echo "checked";}?> class="form">Male</td></tr>

<?php
//reformat date date from YYYY-MM-DD into array(YYYY, MMM, DD)
$membersince = explode("-", $edit['MemberSince']);
?>
<tr><td class="right">Member Since:</td>
<td class="left">
<select name="MemberSinceDay" class="form">
<option value="<?php echo $membersince['2']?>"><?php echo $membersince['2']?></option>
<?php
generateDays();
?>
</select>

<select name="MemberSinceMonth" class="form">
<option value="<?php echo $membersince['1']?>"><?php echo date("F", mktime(0, 0, 0, $membersince['1']+1, 0, 0))?></option>
<?php
generateMonths();
?>
</select>

<select name="MemberSinceYear" class="form">
<option value="<?php echo $membersince['0']?>"><?php echo $membersince['0']?></option>
<?php
generateYears();
?>
</select>
</td></tr>

<tr><td class="right">Status:</td>
<td class="left"><input type="radio" name="Active" value="Y" <?php if ($edit['Active'] == "Y") { echo "checked";}?> class="form">Active<br>
<input type="radio" name="Active" value="N" <?php if ($edit['Active'] == "N") { echo "checked";}?> class="form">Inactive</td>
</tr>

<?php
//reformat units data from km-C-kg into array(km, C, kg)
$units = explode("-", $edit['Measurements']);
?>
<tr><td class="right">Distance Unit:</td>
<td class="left">
<select name="DistanceUnit" class="form">
<?php
distanceUnits($units['0']);
?>
</select>
</td></tr>

<tr><td class="right">Temperature Unit:</td>
<td class="left">
<select name="TemperatureUnit" class="form">
<?php
temperatureUnits($units['1']);
?>
</select>
</td></tr>

<tr><td class="right">Weight Unit:</td>
<td class="left">
<select name="WeightUnit" class="form">
<?php
weightUnits($units['2']);
?>
</select>
</td></tr>

<tr><td class="right">View Mode:</td>
<td class="left"><input type="radio" name="ViewMode" value="0" <?php if (substr($edit['ViewMode'], 0, 1) == "0") { echo "checked"; }?> class="form">Full<br>
<input type="radio" name="ViewMode" value="1" <?php if (substr($edit['ViewMode'], 0, 1) == "1") { echo "checked"; }?> class="form">Summary</td></tr>

<tr><td class="right">Notes:</td>
<td class="left"><textarea name="Notes" rows="4" cols="30" wrap="virtual" class="form"><?php echo htmlentities($edit['Notes'])?></textarea></td></tr>

<tr><td class="right"></td>
<td class="left"><input type="submit" name="submit" value="Submit" accesskey="s" class="form">&nbsp;<input type="submit" value="Wipe" accesskey="w" class="form"></td></tr>


<tr><td class="right" colspan="2"><?php printf("<a href=\"%s?ID=%s&delete=1\" onClick=\"return confirmDelete()\" title=\"Delete Runner: Alt+D\" accesskey=\"d\" class=\"url\"><u>D</u>elete Runner</a>", $_SERVER['PHP_SELF'], $_GET['ID']); ?> | <a href="javascript:window.close();" title="Close This Window: Alt+C" accesskey="c" class="url"><u>C</u>lose</a></td></tr>

</table>
</form>

</div>

<?php
}
?>

</body>
</html>
Return current item: Run