From b0d62dd23fa26479bcba7b0436daffbb4177baf3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Runden?=
Date: Mon, 2 Jan 2023 23:33:10 +0100
Subject: [PATCH] noch c,d,err,r,u
---
README.md | 5 +-
create.php | 116 ++++++++++++++++++++++++++++++++++++
delete.php | 76 ++++++++++++++++++++++++
error.php | 26 ++++++++
read.php | 90 ++++++++++++++++++++++++++++
update.php | 170 +++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 482 insertions(+), 1 deletion(-)
create mode 100644 create.php
create mode 100644 delete.php
create mode 100644 error.php
create mode 100644 read.php
create mode 100644 update.php
diff --git a/README.md b/README.md
index 39cb140..47a150a 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,6 @@
# phpvoctrain
-vocabulary trainer (PHP / Lamp)
\ No newline at end of file
+vocabulary trainer (PHP / Lamp)
+
+ hier geklaut:
+ https://www.tutorialrepublic.com/php-tutorial/php-mysql-crud-application.php
\ No newline at end of file
diff --git a/create.php b/create.php
new file mode 100644
index 0000000..1e0f5a3
--- /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.
+
+
+
+
+
+
+
\ No newline at end of file
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
+
+
+
+
+
+
+
+
+
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
new file mode 100644
index 0000000..943e17e
--- /dev/null
+++ b/read.php
@@ -0,0 +1,90 @@
+
+
+
+
+
+
+ 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
+
+
+
+
+
+
+
\ No newline at end of file