From 51ca61242d1cf55a0448c3efd3cf7e58c36d77a2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Runden?=
Date: Mon, 2 Jan 2023 18:27:52 +0100
Subject: [PATCH] crud complete
---
delete.php | 76 ++++++++++++++++++++++++
error.php | 26 ++++++++
read.php | 58 +++++++++++++++++-
update.php | 170 +++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 329 insertions(+), 1 deletion(-)
create mode 100644 delete.php
create mode 100644 error.php
create mode 100644 update.php
diff --git a/delete.php b/delete.php
new file mode 100644
index 0000000..54bcb83
--- /dev/null
+++ b/delete.php
@@ -0,0 +1,76 @@
+
+
+
+
+
+
+ Delete Record
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/error.php b/error.php
new file mode 100644
index 0000000..e820e61
--- /dev/null
+++ b/error.php
@@ -0,0 +1,26 @@
+
+
+
+
+ Error
+
+
+
+
+
+
+
+
+
Invalid Request
+
Sorry, you've made an invalid request. Please
go back and try again.
+
+
+
+
+
+
\ No newline at end of file
diff --git a/read.php b/read.php
index 3583d1b..943e17e 100644
--- a/read.php
+++ b/read.php
@@ -31,4 +31,60 @@ if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
// URL doesn't contain valid id parameter. Redirect to error page
header("location: error.php");
exit();
- }
\ No newline at end of file
+ }
+
+ } else{
+ echo "Oops! Something went wrong. Please try again later.";
+ }
+ }
+
+ // Close statement
+ mysqli_stmt_close($stmt);
+
+ // Close connection
+ mysqli_close($link);
+} else{
+ // URL doesn't contain id parameter. Redirect to error page
+ header("location: error.php");
+ exit();
+}
+?>
+
+
+
+
+
+ View Record
+
+
+
+
+
+
+
+
+
View Record
+
+
+
+
Back
+
+
+
+
+
+
\ No newline at end of file
diff --git a/update.php b/update.php
new file mode 100644
index 0000000..ee2b148
--- /dev/null
+++ b/update.php
@@ -0,0 +1,170 @@
+array("regexp"=>"/^[a-zA-Z\s]+$/")))){
+ $name_err = "Please enter a valid name.";
+ } else{
+ $name = $input_name;
+ }
+
+ // Validate address 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 update statement
+ $sql = "UPDATE employees SET name=?, address=?, salary=? WHERE id=?";
+
+ if($stmt = mysqli_prepare($link, $sql)){
+ // Bind variables to the prepared statement as parameters
+ mysqli_stmt_bind_param($stmt, "sssi", $param_name, $param_address, $param_salary, $param_id);
+
+ // Set parameters
+ $param_name = $name;
+ $param_address = $address;
+ $param_salary = $salary;
+ $param_id = $id;
+
+ // Attempt to execute the prepared statement
+ if(mysqli_stmt_execute($stmt)){
+ // Records updated 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);
+} else{
+ // Check existence of id parameter before processing further
+ if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
+ // Get URL parameter
+ $id = trim($_GET["id"]);
+
+ // Prepare a select statement
+ $sql = "SELECT * FROM employees WHERE id = ?";
+ if($stmt = mysqli_prepare($link, $sql)){
+ // Bind variables to the prepared statement as parameters
+ mysqli_stmt_bind_param($stmt, "i", $param_id);
+
+ // Set parameters
+ $param_id = $id;
+
+ // Attempt to execute the prepared statement
+ if(mysqli_stmt_execute($stmt)){
+ $result = mysqli_stmt_get_result($stmt);
+
+ if(mysqli_num_rows($result) == 1){
+ /* Fetch result row as an associative array. Since the result set
+ contains only one row, we don't need to use while loop */
+ $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
+
+ // Retrieve individual field value
+ $name = $row["name"];
+ $address = $row["address"];
+ $salary = $row["salary"];
+ } else{
+ // URL doesn't contain valid id. Redirect to error page
+ header("location: error.php");
+ exit();
+ }
+
+ } else{
+ echo "Oops! Something went wrong. Please try again later.";
+ }
+ }
+
+ // Close statement
+ mysqli_stmt_close($stmt);
+
+ // Close connection
+ mysqli_close($link);
+ } else{
+ // URL doesn't contain id parameter. Redirect to error page
+ header("location: error.php");
+ exit();
+ }
+}
+?>
+
+
+
+
+
+ Update Record
+
+
+
+
+
+
+
+
+
Update Record
+
Please edit the input values and submit to update the employee record.
+
+
+
+
+
+
+
\ No newline at end of file