I have a login script which checks the value of a username and password against a database and proceeds to a secure page if the info is correct. I am trying to also set up a link based on the login info which will delete a record in another table (not the one containing the login info.). I think I am about there. I have the Query set-up. I just need help with setting up a link which actually does the deleting when you click it.
The code I have so far...
<?php
ini_set("display_errors","1");
ERROR_REPORTING(E_ALL);
function my_error_handler($errno, $errstr, $errfile, $errline, array $errcontext = array()) {
die($errstr);
}
set_error_handler('my_error_handler');
session_start();
$con = mysql_connect("localhost", "uname", "pw") or trigger_error('Could not connect: ' . mysql_error());
mysql_select_db("nestle_exam", $con) or trigger_error(mysql_error());
class EmptyCredentialsException extends Exception {}
class InvalidCredentialsException extends Exception {}
// Same checking stuff all over again.
function clean($value, $db = null) {
$value = addslashes($value);
return $value;
}
function login($username, $password, $db = null) {
if (empty($username) || empty($password)) {
throw new EmptyCredentialsException();
}
$username = clean($username, $db);
$pwid = clean($password, $db);
$pwid = intval($pwid);
$query = "SELECT name, username FROM TableName WHERE pwid = MD5('$pwid') AND username = '$username'";
$result = mysql_query($query, $db);
if ($result && mysql_num_rows($result)) {
$user = mysql_fetch_assoc($result);
user_update(array('login_timestamp' => time()), $username, $db);
session_regenerate_id();
$meta_data = array('ip' => $_SERVER['REMOTE_ADDR'], 'browser' => $_SEVER['HTTP_USER_AGENT']);
session_store($user + $meta_data);
return true;
}
throw new InvalidCredentialsException();
}
function user_update($data, $username, $db = null) {
$query = 'UPDATE TableName SET ';
$data = array_map('user_update_callback', $data, array_keys($data));
$query = $query . implode(', ', $data);
$query = "$query WHERE username = '$username'";
$result = mysql_query($query, $db) or trigger_error(mysql_error());
return $result && mysql_affected_rows($result);
}
function user_update_callback($value, $key) {
return "$key = '{clean($value)}'";
}
function session_is_auth() {
return (isset($_SESSION['ip']) && isset($_SESSION['browser'])) &&
(($_SESSION['ip'] === $_SERVER['REMOTE_ADDR']) && ($_SESSION['browser'] === $_SERVER['HTTP_USER_AGENT']));
}
function session_store($data) {
$_SESSION = array_merge($_SESSION, $user);
}
if (isset($_POST['submit'])) {
try {
login($_POST['username'], $_POST['pwid']);
} catch (EmptyCredentialsException $e) {
echo "<h2 class='fail'>Please fill in both your username and password to access your exam results.<br />",
"<br >You will be redirected back to the login screen in five seconds.</h2>";
echo "<meta http-equiv='refresh' content='5; url=StudentLogin.php'>";
exit;
} catch (InvalidCredentialsException $e) {
echo "<h2 class='fail'>You have entered a username or password that does not match our database records.",
" please try again.<br><br>You will be redirected back to the login screen in five seconds.</h2> ";
echo "<meta http-equiv='refresh' content='5; url=StudentLogin.php'>";
exit();
}
}
// Start a session. If not logged in will be redirected back to login screen.
if (!session_is_auth()) {
header("Location:StudentLogin.php");
exit;
}
echo "<table id='header'><tr><td align='middle'><div id='welcome'><h3>Welcome! You are now logged in " . $_SESSION['name'] . "</h3></td></tr>";
echo "<tr><td><a class='logout' href='LogoutStudent.php'>Logout</a></td></tr></table>";
$user_id = $_GET['user_id'];
//DELETE QUERY TO SELECT RECORD TO DELETE BASED ON LOGIN INFO.
$query_delete = "DELETE FROM TableName WHERE user_id = $user_id AND TableName.user_id = $user_id";
//echo $query; //for debugging test
$result_delete = mysql_query($query_delete) or die(mysql_error());
?>