diff --git a/assets/php/utils.php b/assets/php/utils.php index aba3f0c..95aa101 100644 --- a/assets/php/utils.php +++ b/assets/php/utils.php @@ -319,8 +319,8 @@ function get_users_cupboards_array() } function delete_cupboard($cupboard_public_id) -{ - global $PDO; +{ + global $PDO; $sql = "DELETE cupboards FROM cupboards @@ -337,8 +337,8 @@ function delete_cupboard($cupboard_public_id) } function delete_product($product_public_id) -{ - global $PDO; +{ + global $PDO; $sql = "DELETE products FROM products @@ -353,3 +353,54 @@ function delete_product($product_public_id) return $query->execute(); } + +function update_product( + $product_public_id, + $new_name, + $new_description, + $new_expiration_date +) { + global $PDO; + + $sql = "UPDATE products + INNER JOIN accounts + ON products.owner_id = accounts.id + SET products.name = :new_name, + products.description = :new_description, + products.expiration_date = :new_expiration_date + WHERE products.public_id = :id + AND products.owner_id = :owner_id;"; + $query = $PDO->prepare($sql); + + $query->bindValue(":new_name", $new_name); + $query->bindValue(":new_description", $new_description); + $query->bindValue(":new_expiration_date", $new_expiration_date); + $query->bindValue(":id", $product_public_id); + $query->bindValue(":owner_id", get_user_info_from_session_id("id")); + + return $query->execute(); +} + +function update_cupboard( + $cupboard_public_id, + $new_name, + $new_description +) { + global $PDO; + + $sql = "UPDATE cupboards + INNER JOIN accounts + ON cupboards.owner_id = accounts.id + SET cupboards.name = :new_name, + cupboards.description = :new_description + WHERE cupboards.public_id = :id + AND cupboards.owner_id = :owner_id;"; + $query = $PDO->prepare($sql); + + $query->bindValue(":new_name", $new_name); + $query->bindValue(":new_description", $new_description); + $query->bindValue(":id", $cupboard_public_id); + $query->bindValue(":owner_id", get_user_info_from_session_id("id")); + + return $query->execute(); +} diff --git a/list-cupboards.php b/list-cupboards.php index db91cde..36d9ac7 100644 --- a/list-cupboards.php +++ b/list-cupboards.php @@ -6,7 +6,7 @@ if (!is_connected()) { header("location: login.php"); } -$erreur = ""; +$erreur = $edit_id = $edit_name = $edit_description = ""; if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["delete"])) { if (!delete_cupboard($_POST["delete"])) { @@ -14,6 +14,27 @@ if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["delete"])) { } } +if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["edit"])) { + if (isset($_POST["edit"])) + foreach (get_users_cupboards_array() as $cupboard) { + if ($cupboard["public_id"] === $_POST["edit"]) { + $edit_id = $_POST["edit"]; + $edit_name = $cupboard["name"]; + $edit_description = $cupboard["description"]; + } + } +} + +if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["edit_completed"])) { + if (isset($_POST["name"]) && isset($_POST["description"])) { + if (!update_cupboard($_POST["edit_completed"], $_POST["name"], $_POST["description"])) { + $erreur = "
Something went wrong. Try again later.
"; + } + } else { + $erreur = "One of the edited section is missing.
"; + } +} + $cupboard_list = ""; foreach (get_users_cupboards_array() as $row) { $cupboard_list = $cupboard_list . "Description | ++ Editer + | Supprimer | diff --git a/list-products.php b/list-products.php index bcb161e..045224e 100644 --- a/list-products.php +++ b/list-products.php @@ -2,16 +2,47 @@ require_once("./assets/php/utils.php"); +$erreur = $edit_id = $edit_name = $edit_description = $edit_expiration = ""; + if (!is_connected()) { header("location: login.php"); } +/** + * $product_public_id, + * $new_name, + * $new_description, + * $new_expiration_date + */ + if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["delete"])) { if (!delete_product($_POST["delete"])) { $erreur = "|
---|---|---|---|
" @@ -25,7 +56,11 @@ foreach (get_users_products_array() as $row) { . " | " . htmlspecialchars($row["cupboard_name"] !== NULL ? $row["cupboard_name"] : "-") . " | " - . "" + . "" + . " | " + . "" . " |
Rangement associƩ | ++ Editer + | Supprimer |
---|