Implement update and delete user

This commit is contained in:
coolneng 2020-06-17 04:10:09 +02:00
parent 219f440b26
commit e6b26d963b
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
7 changed files with 83 additions and 37 deletions

View File

@ -27,14 +27,15 @@ CREATE TABLE IF NOT EXISTS rol(
);
CREATE TABLE IF NOT EXISTS usuario(
id INT AUTO_INCREMENT PRIMARY KEY,
nombre VARCHAR(50) NOT NULL,
usuario VARCHAR(50) PRIMARY KEY,
usuario VARCHAR(50) UNIQUE,
contraseña VARCHAR(100) NOT NULL,
rol INT NOT NULL,
especialidad VARCHAR(50),
correo VARCHAR(100) NOT NULL,
fecha_alta TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
fecha_baja DATE,
fecha_baja TIMESTAMP NULL,
FOREIGN KEY (rol)
REFERENCES rol(codigo)
);

View File

@ -12,22 +12,10 @@ function closeDatabase($pdo) {
$pdo = null;
}
function displayResults($query) {
foreach($query->fetch(PDO::FETCH_ASSOC) as $row){
echo $row . "\n";
}
}
function listRows($pdo, $table) {
$query = "SELECT * FROM ?";
$result = $pdo->prepare($query)->execute([$table]);
displayResults($query);
}
function findRows($pdo, string $table, string $attr, string $param) {
$query = "SELECT * FROM ? WHERE ? = ?";
$result = $pdo->prepare($query)->execute([$table, $attr, $param]);
displayResults($query);
return $result;
}
function createPatient($pdo, $data) {
@ -81,14 +69,18 @@ function createUser($pdo, $data) {
return "Usuario creado con éxito";
}
function editUser($pdo, $attr, $param, $id) {
$query = "UPDATE usuario SET ? = ? WHERE usuario = ?";
$pdo->prepare($query)->execute([$attr, $param, $id]);
function editUser($pdo, $data, $id) {
$query = "UPDATE usuario SET nombre=?, usuario=?, contraseña=?, rol=?, correo=? WHERE id=?";
$result = $pdo->prepare($query);
$result->execute([$data["nombre"], $data["usuario"], $data["contraseña"], $data["rol"], $data["correo"], $id]);
$result->debugDumpParams;
return "Usuario modificado con éxito";
}
function deactivateUser($pdo, string $id) {
editUser($pdo, "fecha_baja", "CURRENT_TIMESTAMP", $id);
function deactivateUser($pdo, $data, $id) {
$query = "UPDATE usuario SET fecha_baja=now() WHERE id=?";
$result = $pdo->prepare($query);
$result->execute([$id]);
return "Usuario desactivado con éxito";
}
@ -96,18 +88,18 @@ function listAppointments($pdo, string $doctor) {
findRows($pdo, "cita", "medico", $doctor);
}
function displayUsers($query) {
foreach($query->fetch(PDO::FETCH_ASSOC) as $row){
echo $row["nombre"];
}
}
function listUsers($pdo) {
$query = "SELECT usuario.nombre, usuario.usuario, rol.nombre, usuario.correo FROM usuario
$query = "SELECT usuario.nombre, usuario.usuario, rol.nombre, usuario.correo, usuario.id, fecha_baja FROM usuario
INNER JOIN rol ON usuario.rol = rol.codigo";
$result = $pdo->query($query)->fetchAll();
return $result;
}
function findUser($pdo, string $id) {
$query = "SELECT * FROM usuario WHERE id=?";
$result = $pdo->prepare($query);
$result->execute([$id]); $data = $result->fetch();
return $data;
}
?>

View File

@ -32,7 +32,7 @@
<input type="text" name="correo" value="">
</div>
<div class="input-group">
<button class="btn" type="submit" name="submit" >Guardar</button>
<button class="btn" type="submit" name="create" >Guardar</button>
</div>
</form>
</body>

View File

@ -0,0 +1,45 @@
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Gestión de usuarios</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../../static/style.css" type="text/css" media="screen" />
</head>
<body>
<?php
include '../database.php';
$pdo = connectDatabase("practica", "practica", "practica");
$data = findUser($pdo, $_GET["edit"]);
?> <form method="post" action="../user_management.php">
<div class="input-group">
<label>Nombre</label>
<input type="text" name="nombre" value="<?php echo $data[1]; ?>">
</div>
<div class="input-group">
<label>usuario</label>
<input type="text" name="usuario" value="<?php echo $data[2]; ?>">
</div>
<div class="input-group">
<label>contraseña</label>
<input type="password" name="contraseña" value="">
</div>
<div class="select-input">
<label>rol</label>
<select id="rol" name="rol">
<option value="1">administrativo</option>
<option value="2">médico</option>
</select>
</div>
<div class="input-group">
<label>correo</label>
<input type="text" name="correo" value="<?php echo $data[6]; ?>">
</div>
<div class="input-group">
<button class="btn" type="submit" name="edit" >Editar</button>
</div>
<input type="hidden" name="id" value="<?php echo $_GET["edit"]; ?>">
</form>
<?php closeDatabase($pdo); ?>
</body>

View File

@ -25,6 +25,7 @@
$pdo = connectDatabase("practica", "practica", "practica");
$list = listUsers($pdo);
foreach($list as $row) :
if(!$row[5]):
?>
<tr>
<td><?php echo $row[0]; ?></td>
@ -32,16 +33,18 @@
<td><?php echo $row[2]; ?></td>
<td><?php echo $row[3]; ?></td>
<td>
<a href="html/user_create_form.html?edit=" class="edit_btn">Editar</a>
<a href="forms/user_edit_form.php?edit=<?php echo $row[4]; ?>" class="edit_btn">Editar</a>
</td>
<td>
<a href="html/user_create_form.html?delete=" class="del_btn">Borrar</a>
<a href="user_management.php?delete=<?php echo $row[4]; ?>" class="del_btn">Borrar</a>
</td>
</tr>
<?php endif ?>
<?php endforeach ?>
<?php closeDatabase($pdo); ?>
</table>
<div style="text-align: right;">
<a href="html/user_create_form.html" class="create_btn" >Crear</a>
<a href="forms/user_create_form.html" class="create_btn" >Crear</a>
</div>
</body>
</html>

View File

@ -4,33 +4,38 @@ include'database.php';
function userCreation(){
$data = $_POST;
$pdo = connectDatabase("practica", "practica", "practica");
$message = createUser($pdo, $data);
createUser($pdo, $data);
closeDatabase($pdo);
header('location: user.php');
}
function userModification(){
$id = $_GET["edit"];
$data = $_POST;
$id = $_POST["id"];
$pdo = connectDatabase("practica", "practica", "practica");
editUser($pdo, $data, $id);
closeDatabase($pdo);
header('location: user.php');
}
function userDeletion(){
$id = $_GET["delete"];
$pdo = connectDatabase("practica", "practica", "practica");
deactivateUser($pdo, $data, $id);
closeDatabase($pdo);
header('location: user.php');
}
if (isset($_POST['submit'])) {
if (isset($_POST["create"])) {
userCreation();
}
if (isset($_GET['edit'])) {
if (isset($_POST["edit"])) {
userModification();
}
if (isset($_GET['delete'])) {
if (isset($_GET["delete"])) {
userDeletion();
}