From 726f92798791aebe5528c096dd0cf9d2515d637a Mon Sep 17 00:00:00 2001
From: Joshua Perry <45966243+jpez-development@users.noreply.github.com>
Date: Sun, 20 Nov 2022 13:20:58 +0000
Subject: [PATCH] updated pdo
---
functions.php | 15 ++++++++++-----
layout.php | 2 +-
public/account/login.php | 7 ++-----
public/account/register.php | 8 +++-----
public/account/settings.php | 0
public/admin/addCategory.php | 6 +-----
public/admin/deleteCategory.php | 6 +-----
public/admin/editCategory.php | 6 +-----
public/index.php | 9 ++-------
public/listing.php | 8 ++------
10 files changed, 23 insertions(+), 44 deletions(-)
delete mode 100644 public/account/settings.php
diff --git a/functions.php b/functions.php
index 66eb3c7..8fddea8 100644
--- a/functions.php
+++ b/functions.php
@@ -1,10 +1,6 @@
prepare('SELECT * FROM category');
$stmt->execute();
$cats = $stmt->fetchAll();
@@ -22,4 +18,13 @@ function adminCheck() {
echo'';
}
}
+
+function startDB() {
+ $server = 'mysql';
+ $username = 'student';
+ $password = 'student';
+ $schema = 'assignment1';
+ $pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password);
+ return $pdo;
+}
?>
\ No newline at end of file
diff --git a/layout.php b/layout.php
index 2e6e524..9381337 100644
--- a/layout.php
+++ b/layout.php
@@ -49,7 +49,7 @@ require_once 'functions.php';
echo $pageContent;
?>
diff --git a/public/account/login.php b/public/account/login.php
index dd5e16a..a81a3c1 100644
--- a/public/account/login.php
+++ b/public/account/login.php
@@ -10,12 +10,9 @@ $pageContent = '
Don\'t have an account?Click here to
';
$stylesheet = '../assets/ibuy.css';
require '../../layout.php';
+require_once '../../functions.php';
-$server = 'mysql';
-$username = 'student';
-$password = 'student';
-$schema = 'assignment1';
-$pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password);
+$pdo = startDB();
if (isset($_POST['submit'])) {
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = :email');
diff --git a/public/account/register.php b/public/account/register.php
index 918977d..55f6e67 100644
--- a/public/account/register.php
+++ b/public/account/register.php
@@ -1,10 +1,8 @@
prepare('INSERT INTO users (first_name, last_name, email, password, admin)
VALUES (:first_name, :last_name, :email, :password, :admin)');
diff --git a/public/account/settings.php b/public/account/settings.php
deleted file mode 100644
index e69de29..0000000
diff --git a/public/admin/addCategory.php b/public/admin/addCategory.php
index 895c505..873eec3 100644
--- a/public/admin/addCategory.php
+++ b/public/admin/addCategory.php
@@ -12,11 +12,7 @@ $pageContent = ' Add Category
require '../../layout.php';
if (isset($_POST['submit'])) {
- $server = 'mysql';
- $username = 'student';
- $password = 'student';
- $schema = 'assignment1';
- $pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password);
+ $pdo = startDB();
$stmt = $pdo->prepare('INSERT INTO category(name)
VALUES(:name)');
$values = [
diff --git a/public/admin/deleteCategory.php b/public/admin/deleteCategory.php
index 3e47ade..fd3d73b 100644
--- a/public/admin/deleteCategory.php
+++ b/public/admin/deleteCategory.php
@@ -6,11 +6,7 @@ require_once '../../functions.php';
adminCheck();
if (isset($_GET['category_id'])) {
- $server = 'mysql';
- $username = 'student';
- $password = 'student';
- $schema = 'assignment1';
- $pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password);
+ $pdo = startDB();
$stmt = $pdo->prepare('DELETE FROM category WHERE category_id= :category_id');
$values = [
'category_id' => $_GET['category_id']
diff --git a/public/admin/editCategory.php b/public/admin/editCategory.php
index af33a9e..4b07bd1 100644
--- a/public/admin/editCategory.php
+++ b/public/admin/editCategory.php
@@ -15,11 +15,7 @@ if (isset($_GET['category_id'])) {
$_SESSION['cat_id'] = $_GET['category_id'];
}
else if (isset($_POST['submit'])) {
- $server = 'mysql';
- $username = 'student';
- $password = 'student';
- $schema = 'assignment1';
- $pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password);
+ $pdo = startDB();
$stmt = $pdo->prepare('UPDATE category SET name= :cat_name WHERE category_id= :category_id');
$values = [
'cat_name' => $_POST['name'],
diff --git a/public/index.php b/public/index.php
index da021ef..39516be 100644
--- a/public/index.php
+++ b/public/index.php
@@ -10,20 +10,15 @@ if (isset($_GET['pageHeading'])) {
else {
$pageHeading = 'Latest Listings';
}
+require_once '../functions.php';
$pageContent = ''.$pageHeading.'
'.populateList($pageHeading).'
';
require '../layout.php';
-
function populateList($category) {
+ $pdo = startDB();
$output = '';
- $server = 'mysql';
- $username = 'student';
- $password = 'student';
- $schema = 'assignment1';
- $pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password);
-
if ($category === 'Latest Listings') {
$stmt = $pdo->prepare('SELECT * FROM auction WHERE endDate > "'. date("Y-m-d H:i:s"). '" ORDER BY endDate DESC');
$stmt->execute();
diff --git a/public/listing.php b/public/listing.php
index 974db00..dfa653f 100644
--- a/public/listing.php
+++ b/public/listing.php
@@ -4,15 +4,11 @@ $pageContent = 'Product Page
'. populateContent() .'';
require '../layout.php';
-
+require_once '../functions.php';
function populateContent() {
- $server = 'mysql';
- $username = 'student';
- $password = 'student';
- $schema = 'assignment1';
- $pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password);
+ $pdo = startDB();
$stmt = $pdo->prepare('SELECT * FROM auction WHERE listing_id= :listing_id');
$values = [