From 2b9b667bdd3de87dc97f58e13975391f7649cb87 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Tue, 12 Nov 2019 00:35:30 +0100 Subject: [PATCH] added a way to delete cupboards --- assets/php/utils.php | 14 +++++++++++++- list-cupboards.php | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/assets/php/utils.php b/assets/php/utils.php index 7c9fd3e..ef54e79 100644 --- a/assets/php/utils.php +++ b/assets/php/utils.php @@ -305,7 +305,7 @@ function get_users_cupboards_array() $user_cupboards = array(); $sql = "SELECT - id, name, description, public_id + id, name, description, cupboards.public_id AS public_id FROM cupboards WHERE owner_id = :owner_id;"; $query = $PDO->prepare($sql); @@ -317,3 +317,15 @@ function get_users_cupboards_array() } return $user_cupboards; } + +function delete_cupboard($cupboard_public_id) +{ + global $PDO; + + $sql = "DELETE cupboards FROM cupboards INNER JOIN accounts ON cupboards.owner_id = accounts.id WHERE cupboards.public_id = :id;"; + $query = $PDO->prepare($sql); + + $query->bindValue(":id", $cupboard_public_id); + + return $query->execute(); +} diff --git a/list-cupboards.php b/list-cupboards.php index b07cf0d..4ba93d0 100644 --- a/list-cupboards.php +++ b/list-cupboards.php @@ -6,12 +6,22 @@ if (!is_connected()) { header("location: login.php"); } +$erreur = ""; + +if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["delete"])) { + if (!delete_cupboard($_POST["delete"])) { + $erreur = "

An item is in this cupboard, you can't delete it now.

\n"; + } +} + $cupboard_list = ""; foreach (get_users_cupboards_array() as $row) { $cupboard_list = $cupboard_list . "" . htmlspecialchars($row["name"]) . "" . htmlspecialchars($row["description"]) + . "" + . "
" . "\n"; } @@ -35,6 +45,7 @@ foreach (get_users_cupboards_array() as $row) { + @@ -44,6 +55,9 @@ foreach (get_users_cupboards_array() as $row) { +
Description + Supprimer +