From e6b26d963beacd12fec6f487135d6fc3aa9a3654 Mon Sep 17 00:00:00 2001 From: coolneng Date: Wed, 17 Jun 2020 04:10:09 +0200 Subject: [PATCH] Implement update and delete user --- database/db.sql | 5 ++- src/database.php | 44 +++++++++------------- src/{html => forms}/header-footer.html | 0 src/{html => forms}/user_create_form.html | 2 +- src/forms/user_edit_form.php | 45 +++++++++++++++++++++++ src/user.php | 9 +++-- src/user_management.php | 15 +++++--- 7 files changed, 83 insertions(+), 37 deletions(-) rename src/{html => forms}/header-footer.html (100%) rename src/{html => forms}/user_create_form.html (95%) create mode 100644 src/forms/user_edit_form.php diff --git a/database/db.sql b/database/db.sql index 751db5a..4b1d648 100644 --- a/database/db.sql +++ b/database/db.sql @@ -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) ); diff --git a/src/database.php b/src/database.php index 8beb84c..993463e 100644 --- a/src/database.php +++ b/src/database.php @@ -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; +} + ?> diff --git a/src/html/header-footer.html b/src/forms/header-footer.html similarity index 100% rename from src/html/header-footer.html rename to src/forms/header-footer.html diff --git a/src/html/user_create_form.html b/src/forms/user_create_form.html similarity index 95% rename from src/html/user_create_form.html rename to src/forms/user_create_form.html index 6149c8a..92533a6 100644 --- a/src/html/user_create_form.html +++ b/src/forms/user_create_form.html @@ -32,7 +32,7 @@
- +
diff --git a/src/forms/user_edit_form.php b/src/forms/user_edit_form.php new file mode 100644 index 0000000..60a8b76 --- /dev/null +++ b/src/forms/user_edit_form.php @@ -0,0 +1,45 @@ + + + + Gestión de usuarios + + + + + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+ "> +
+ + diff --git a/src/user.php b/src/user.php index 6ad354a..7920e9b 100644 --- a/src/user.php +++ b/src/user.php @@ -25,6 +25,7 @@ $pdo = connectDatabase("practica", "practica", "practica"); $list = listUsers($pdo); foreach($list as $row) : + if(!$row[5]): ?> @@ -32,16 +33,18 @@ - Editar + Editar - Borrar + Borrar + +
- Crear + Crear
diff --git a/src/user_management.php b/src/user_management.php index da9555b..75c66d2 100644 --- a/src/user_management.php +++ b/src/user_management.php @@ -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(); }