From ee0d392ac8a08b1a3af82dc530e55e09f413df42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Runden?= Date: Mon, 2 Jan 2023 18:18:04 +0100 Subject: [PATCH] doit --- config.php | 2 +- create.php | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 create.php diff --git a/config.php b/config.php index 9b87223..9fae02a 100644 --- a/config.php +++ b/config.php @@ -15,4 +15,4 @@ $link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME); if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); } -?> \ No newline at end of file +?> diff --git a/create.php b/create.php new file mode 100644 index 0000000..a07c9c9 --- /dev/null +++ b/create.php @@ -0,0 +1,116 @@ +array("regexp"=>"/^[a-zA-Z\s]+$/")))){ + $name_err = "Please enter a valid name."; + } else{ + $name = $input_name; + } + + // Validate address + $input_address = trim($_POST["address"]); + if(empty($input_address)){ + $address_err = "Please enter an address."; + } else{ + $address = $input_address; + } + + // Validate salary + $input_salary = trim($_POST["salary"]); + if(empty($input_salary)){ + $salary_err = "Please enter the salary amount."; + } elseif(!ctype_digit($input_salary)){ + $salary_err = "Please enter a positive integer value."; + } else{ + $salary = $input_salary; + } + + // Check input errors before inserting in database + if(empty($name_err) && empty($address_err) && empty($salary_err)){ + // Prepare an insert statement + $sql = "INSERT INTO employees (name, address, salary) VALUES (?, ?, ?)"; + + if($stmt = mysqli_prepare($link, $sql)){ + // Bind variables to the prepared statement as parameters + mysqli_stmt_bind_param($stmt, "sss", $param_name, $param_address, $param_salary); + + // Set parameters + $param_name = $name; + $param_address = $address; + $param_salary = $salary; + + // Attempt to execute the prepared statement + if(mysqli_stmt_execute($stmt)){ + // Records created successfully. Redirect to landing page + header("location: index.php"); + exit(); + } else{ + echo "Oops! Something went wrong. Please try again later."; + } + } + + // Close statement + mysqli_stmt_close($stmt); + } + + // Close connection + mysqli_close($link); +} +?> + + + + + + Create Record + + + + +
+
+
+
+

Create Record

+

Please fill this form and submit to add employee record to the database.

+
" method="post"> +
+ + + +
+
+ + + +
+
+ + + +
+ + Cancel +
+
+
+
+
+ + \ No newline at end of file