From f220852d3283f1e5852bb7f867866ae43f177c21 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Sun, 10 Nov 2019 14:10:10 +0100 Subject: [PATCH] added a way to add a product --- add-produit.php | 149 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 add-produit.php diff --git a/add-produit.php b/add-produit.php new file mode 100644 index 0000000..39e121b --- /dev/null +++ b/add-produit.php @@ -0,0 +1,149 @@ + 255) { + $product_name_err = "Max size for the product name is 255 characters." + . "Yours was " . strlen(trim($_POST["name"])) . " characters long."; + } else { + $product_name = trim($_POST["name"]); + } + + + // DESCRIPTION + if (empty(trim($_POST["description"]))) { + $description = null; + } else if (strlen(trim($_POST["description"])) > 65535) { + $description_err = "Your description is too long."; + } else { + $description = trim($_POST["description"]); + } + + + // EXPIRATION DATE + if (empty(trim($_POST["date"]))) { + $expiration_date = null; + } else if (date_format(date_create($_POST["date"]), 'Y-m-d') == $_POST["date"]) { + $min_date = date_format(date_create("01/01/1000"), 'Y-m-d'); + $max_date = date_format(date_create("12/31/9999"), 'Y-m-d'); + if ((trim($_POST["date"]) >= $min_date) && (trim($_POST["date"]) <= $max_date)) { + $expiration_date = trim($_POST["date"]); + } else { + $expiration_date_err = "The date isn't in our correct expiration date range."; + } + } else { + $expiration_date_err = "The date is invalid."; + } + + // CUPBOARD + if (empty(trim($_POST["cupboard"]))) { + $cupboard = null; + } else if (is_numeric(trim($_POST["cupboard"]))) { + $cupboard_id = trim($_POST["cupboard"]); + if (does_cupboard_exist_from_id($cupboard_id)) { + $cupboard = trim($_POST["cupboard"]); + } else { + $cupboard_err = "Unknown cupboard."; + } + } else { + $cupboard_err = "Cupboard id isn't int."; + } + + // ======================================================================== + + // ======================================================================== + // INSERTION IN DATABASE IF CORRECT + // ======================================================================== + if (empty($product_name_err) && empty($description_err) && empty($expiration_date_err) && empty($cupboard_err)) { + + + + if (!add_product($product_name, $description, $expiration_date, $cupboard_id)) { + echo "Error. Something went wrong."; + } + } else { + + echo $product_name_err; + echo $description_err; + echo $expiration_date_err; + echo $cupboard_err . $_POST["cupboard"]; + } + exit(); +} + +// ============================================================================= +// BUILD CUPBOARD LIST FROM DATABASE +// ============================================================================= + +$cupboard_list = ""; +foreach (get_users_cupboards_array() as $row) { + $cupboard_list = $cupboard_list . "\n"; +} +?> + + + + + + + + + Document + + + +
" method="post"> +
+ + + +
+
+ + + +
+
+ + + +
+
+ + +
+
+ +
+
+ + + \ No newline at end of file