food-inventory/add-cupboard.php
2019-11-11 15:00:26 +01:00

87 lines
3.1 KiB
PHP

<?php
require_once("./assets/php/utils.php");
// Check if the user is logged in, if not then redirect him to login page
if (!is_connected()) {
header("location: login.php");
exit;
}
// ========================================================================
// IF IN POST : USER SUBMITTED THE FORM
// ========================================================================
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Set all variables to empty.
$cupboard_name = $cupboard_description = "";
$cupboard_name_err = $cupboard_description_err = "";
// ========================================================================
// USER INPUT VALIDATION AND SANITIZATION PROCESS
// ========================================================================
// Check and store every field the user entered.
// NAME
if (empty(trim($_POST["name"]))) {
$cupboard_name_err = "Please input a cupboard name.";
} else if (strlen(trim($_POST["name"])) > 255) {
$cupboard_name_err = "Max size for the cupboard name is 255 characters. "
. "Yours was " . strlen(trim($_POST["name"])) . " characters long.";
} else {
$cupboard_name = trim($_POST["name"]);
}
// DESCRIPTION
if (empty(trim($_POST["description"]))) {
$cupboard_description = null;
} else if (strlen(trim($_POST["description"])) > 65535) {
$cupboard_description_err = "Your description is too long.";
} else {
$cupboard_description = trim($_POST["description"]);
}
// ========================================================================
// ========================================================================
// INSERTION IN DATABASE IF CORRECT
// ========================================================================
if (empty($cupboard_name_err) && empty($cupboard_description_err)) {
if (!add_cupboard($cupboard_name, $cupboard_description)) {
echo "Oops! Something went wrong. Please try again later.";
}
} else {
echo $cupboard_name_err;
echo $cupboard_description_err;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
<label>Nom</label>
<input type="text" name="name" class="form-control" value="" required>
<span class="help-block"></span>
</div>
<div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
<label>Description</label>
<input type="text" name="description" class="form-control" value="">
<span class="help-block"></span>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Test">
</div>
</form>
</body>
</html>