Skip to content

Instantly share code, notes, and snippets.

@samroar
Created November 19, 2014 19:07
Show Gist options
  • Save samroar/ce240538f938aaa875a4 to your computer and use it in GitHub Desktop.
Save samroar/ce240538f938aaa875a4 to your computer and use it in GitHub Desktop.
<?php
include('connection.php');
include('restrict-login.php');
if(isset($_POST['action'])) {
if($_POST['action'] == 'add') {
$parameters = array($_POST['firstName'], $_POST['lastName'], $_POST['sex'], $_POST['dob'], $_POST['email'], $_POST['password'], $_POST['type'], $_POST['phoneNo']);
if($_POST['what'] == 'admin') {
$query = $dbh->prepare('INSERT INTO users (firstName, lastName, sex, dob, email, type, password, phoneNo) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
} elseif($_POST['what'] == 'patient') {
$query = $dbh->prepare('INSERT INTO users (firstName, lastName, sex, dob, email, type, password, phoneNo, experience) VALUES (?, ?, ?, ?, ?, ?, ?, ?)');
$parameters[] = $_POST['experience'];
} elseif($_POST['what'] == 'admin') {
$query = $dbh->prepare('INSERT INTO users (firstName, lastName, sex, dob, email, type, password, phoneNo, history, bloodGroup) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)');
$parameters[] = $_POST['history'];
$parameters[] = $_POST['bloodGroup'];
}
$query->execute($parameters);
}
if($_POST['action'] == 'edit') {
$parameters = array($_POST['firstName'], $_POST['lastName'], $_POST['sex'], $_POST['dob'], $_POST['email'], $_POST['password'], $_POST['phoneNo']);
if($_POST['what'] == 'admin') {
$query = $dbh->prepare('UPDATE users SET firstName = ?, lastName = ?, sex = ?, dob = ?, email = ?, password = ?, phoneNo = ? WHERE id = ?');
} elseif($_POST['what'] == 'doctor') {
$query = $dbh->prepare('UPDATE users SET firstName = ?, lastName = ?, sex = ?, dob = ?, email = ?, password = ?, phoneNo = ?, experience = ? WHERE id = ?');
$parameters[] = $_POST['experience'];
} elseif($_POST['what'] == 'patient') {
$query = $dbh->prepare('UPDATE users SET firstName = ?, lastName = ?, sex = ?, dob = ?, email = ?, password = ?, phoneNo = ?, history = ?, bloodGroup = ? WHERE id = ?');
$parameters[] = $_POST['history'];
$parameters[] = $_POST['bloodGroup'];
}
$parameters[] = $_POST['id'];
$query->execute($parameters);
}
if($_POST['action'] == 'delete') {
if($_POST['what'] == "admin") {
$query = $dbh->prepare('DELETE FROM users WHERE id = ?');
} elseif($_POST['what'] == 'doctor') {
$query = $dbh->prepare('DELETE FROM users WHERE id = ?');
} elseif($_POST['what'] == 'patient') {
$query = $dbh->prepare('DELETE FROM users WHERE id = ?');
}
$query->execute();
}
}
if($_SESSION['type'] == 0) {
$rows = $dbh->query('SELECT firstName, lastName, type FROM users');
$type = array('Admin', 'Doctor', 'Patient');
?><!DOCTYPE html>
<html>
<head>
<title>users</title>
</head>
<body>
<table>
<tr>
<th>Type</th>
<th>Name</th>
<th>Edit</th>
</tr>
<?php foreach($rows as $row) { ?>
<tr>
<td><?php echo $type[$row['type']]; ?></td>
<td><?php echo $row['firstName'].' '.$row['lastName'] ?></td>
<td><a href="?id=<?php echo $row['id']; ?>">Edit</a></td>
</tr>
<?php } ?>
</table>
</body>
</html>
<?php } ?>
<!DOCTYPE html>
<html>
<head>
<title>users</title>
</head>
<body>
<?php
$user = false;
if($_GET['action'] == 'edit' && $_SESSION['type'] > 0) {
$_GET['id'] = $_SESSION['id'];
}
if($_GET['action'] == 'edit') {
$dbh->prepare('SELECT * FROM users WHERE id = ?');
$dbh->execute($_GET['id']);
$user = $dbh->fetch();
}
?>
<form action="" method="POST">
<label><br>First Name</br><input type = "text" name = "firstName" value="<?php if($user) echo $user['firstName']; ?>"></label>
<label><br>Last Name</br><input type = "text" name = "lastName" value="<?php if($user) echo $user['lastName']; ?>"></label>
<label><br>Sex</br><input type = "text" name = "sex" value="<?php if($user) echo $user['sex']; ?>"></label>
<label><br>DOB</br><input type = "date" name = "dob" value="<?php if($user) echo $user['dob']; ?>"></label>
<label><br>Email</br><input type = "email" name = "email" value="<?php if($user) echo $user['email']; ?>"></label>
<label><br>Type</br><input type = "text" name = "type"></label>
<label><br>Password</br><input type = "password" name = "password"></label>
<?php if($_GET['what'] == 'doctor') { ?>
<label><br>Experience</br><input type = "number" name = "experience" value="<?php if($user) echo $user['experience']; ?>"></label>
<?php } ?>
<?php if($_GET['what'] == 'patient') { ?>
<label><br>History</br><input type = "text" name = "history" value="<?php if($user) echo $user['history']; ?>"></label>
<label><br>Blood Group</br><input type = "text" name = "bloodGroup" value="<?php if($user) echo $user['bloodGroup']; ?>"></label>
<?php } ?>
<input type = "hidden" name = "action" value = "<?php echo $_GET['action']; ?>">
<input type = "hidden" name = "what" value = "<?php echo $_GET['what']; ?>">
<?php if($_GET['action'] == 'edit') { ?>
<input type = "hidden" name = "id" value = "<?php echo $_GET['id']; ?>">
<?php } ?>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment