From 2ed23b835735c516b26d3125708ab53238ae6147 Mon Sep 17 00:00:00 2001 From: silviooosilva Date: Fri, 2 Sep 2022 03:21:23 +0100 Subject: [PATCH 1/2] Implementing PDO , reducing code and applying filters for security --- addDetails.php | 50 +++++----- app/connection.php | 11 +++ app/helpers.php | 12 +++ dashboard.php | 160 +++++++++++++++---------------- delete.php | 35 +++---- devDetails.php | 127 +++++++++++++------------ update.php | 230 ++++++++++++++++++++++----------------------- 7 files changed, 316 insertions(+), 309 deletions(-) create mode 100644 app/connection.php create mode 100644 app/helpers.php diff --git a/addDetails.php b/addDetails.php index 04f228b..fb79396 100644 --- a/addDetails.php +++ b/addDetails.php @@ -1,33 +1,31 @@ -alert('Details Inserted')"; - echo ""; - } +if (isset($_POST['submit'])) { + $firstName = filter($_POST['firstName']); + $lastName = filter($_POST['lastName']); + $email = filter($_POST['email'])/* */; - else - { - echo "Unable to insert the details"; - } - } - ?> \ No newline at end of file + $query = $pdo->prepare("INSERT INTO developerdetails (firstName, LastName, email) VALUES (?, ?, ?)"); + $query->bindParam(1, $firstName); + $query->bindParam(2, $lastName); + $query->bindParam(3, $email); + + if ($query->execute()) { + echo ""; + echo ""; + } else { + echo "Unable to insert the details"; + } +} diff --git a/app/connection.php b/app/connection.php new file mode 100644 index 0000000..f055386 --- /dev/null +++ b/app/connection.php @@ -0,0 +1,11 @@ + "SET NAMES utf8", + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION +)); diff --git a/app/helpers.php b/app/helpers.php new file mode 100644 index 0000000..4a8e795 --- /dev/null +++ b/app/helpers.php @@ -0,0 +1,12 @@ + + - - - Dashboard - - + + + Dashboard + + - -
- -
-
-

Developer Details

-
-
- - - - - - - - - - - +
+ +
+
+

Developer Details

+
+
+
FirstnameLastnameEmailOperation
+ + + + + + + + - $query="select * from developerdetails"; - $run=mysqli_query($con,$query); - while($row=mysqli_fetch_array($run)) - { - $id=$row['id']; - $firstName=$row['firstName']; - $lastName=$row['LastName']; - $email=$row['email']; - - -?> + prepare("SELECT * FROM developerdetails"); + $query->execute(); + while ($row = $query->fetch(PDO::FETCH_ASSOC)) { + ?> - - - - - - - - - -
FirstnameLastnameEmailOperation
- Read - Update - Delete -
-
+ + + + + + + Read + Update + Delete + + + + + + - + - + \ No newline at end of file diff --git a/delete.php b/delete.php index a752449..93c478f 100644 --- a/delete.php +++ b/delete.php @@ -1,26 +1,15 @@ -alert('Details Deleted')"; - echo ""; - } - - else - { - echo "Unable to Delete the details"; - } - - - ?> \ No newline at end of file +$delete_id = filter($_GET['deleteid']); +$query = $pdo->prepare("DELETE FROM developerdetails WHERE id = ?"); +$query->bindParam(1, $delete_id); +if ($query->execute()) { + echo ""; + echo ""; +} else { + echo "Unable to Delete the details"; +} diff --git a/devDetails.php b/devDetails.php index 5322b3b..336cd02 100644 --- a/devDetails.php +++ b/devDetails.php @@ -1,67 +1,76 @@ prepare("SELECT * FROM developerdetails WHERE id = ?"); +$query->bindParam(1, $read_details); +$query->execute(); + +while ($row = $query->fetch(PDO::FETCH_ASSOC)) { + $update_id = filter($row['id']); + $firstName = filter($row['firstName']); + $lastName = filter($row['LastName']); + $email = filter($row['email']); +} + ?> - - - - View Details - - - - - -
- -
- -

Dev Details

- -
-

Hi!
- My name is . My employee id is . You can contact me at - . -

-
- - - - - \ No newline at end of file + + + +

Dev Details

+ +
+

Hi!
+ My name is . My employee id is . You can contact me at + . +

+
+ + + + + + \ No newline at end of file diff --git a/update.php b/update.php index 803eb73..7248973 100644 --- a/update.php +++ b/update.php @@ -1,130 +1,124 @@ - prepare("SELECT * from developerdetails WHERE id = ?"); + $query->bindParam(1, $update_details); + $query->execute(); + + while ($row = $query->fetch(PDO::FETCH_ASSOC)) { + $update_id = filter($row['id']); + $firstName = filter($row['firstName']); + $lastName = filter($row['LastName']); + $email = filter($row['email']); + } } ?> - - - - Update Details - - - - - -
- -
-

Update Details

-
-
- -
- " - id="firstName" placeholder="Enter you first name" required> -
-
-
- -
- -
-
-
- -
- -
-
-
-
- -
-
-
- - - - - - + +

Update Details

+
+
+ +
+ " id="firstName" placeholder="Enter you first name" required> +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+ +
+
+
+ + + + + + +prepare("UPDATE developerdetails SET firstName = ?, LastName = ?, email = ? WHERE id = ?"); + $query_update->bindParam(1, $firstName); + $query_update->bindParam(2, $lastName); + $query_update->bindParam(3, $email); + $query_update->bindParam(4, $update_id); + + if ($query_update->execute()) { echo ""; echo ""; - } - else - { + } else { echo "Unable to Update the details"; - } - } - ?> \ No newline at end of file + } +} +?> \ No newline at end of file From 63ccb6243682458c03936b6841cc146212a00f01 Mon Sep 17 00:00:00 2001 From: silviooosilva Date: Fri, 2 Sep 2022 03:23:40 +0100 Subject: [PATCH 2/2] Implementing PDO , reducing code and applying filters for security --- dashboard.php | 2 +- devDetails.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dashboard.php b/dashboard.php index 4c6f1fa..9f6a666 100644 --- a/dashboard.php +++ b/dashboard.php @@ -32,7 +32,7 @@ function logOut() {