diff --git a/functions.php b/functions.php index 7956451..f67947e 100644 --- a/functions.php +++ b/functions.php @@ -1,25 +1,22 @@ prepare('SELECT * FROM category'); - $stmt->execute(); +function fetchCats() { //get all categories $cats = executeQueryWithoutConstraint('category','*')->fetchAll(); - return $cats; } -function adminCheck() { +function adminCheck() { //check to see if user is logged in as admin if(isset($_SESSION['admin'])) { if($_SESSION['admin'] != 'y') { - echo ''; + echo ''; //redirect } } else { - echo''; + echo''; //redirect } } -function startDB() { // Code for connecting to the database from https://www.sitepoint.com/re-introducing-pdo-the-right-way-to-access-databases-in-php/ +function startDB() { //Create a db connection + // Code for connecting to the database from https://www.sitepoint.com/re-introducing-pdo-the-right-way-to-access-databases-in-php/ $server = 'mysql'; $username = 'student'; $password = 'student'; @@ -28,23 +25,23 @@ function startDB() { // Code for connecting to the database from https://www.sit return $pdo; } -function checkListing() { +function checkListing() { //check if the get variables contains listing_id if (!isset($_GET['listing_id'])) { echo ''; } } -function checkId() { +function checkId() { //check if the get variables contains user_id if (!isset($_GET['user_id'])) { echo ''; } } -function getListing() { +function getListing() { //get listing that matches listing_id stored in the get variables return getFirstAllMatches('auction', 'listing_id', $_GET['listing_id']); } -function populateCatSelect() { +function populateCatSelect() { //Populate a select input with all categories $cats = fetchCats(); $output = ''; foreach ($cats as &$cat) { @@ -53,7 +50,7 @@ function populateCatSelect() { return $output; } -function executeQuery($tableName, $colName, $constraintCol, $constraint) { +function executeQuery($tableName, $colName, $constraintCol, $constraint) { //execute a SELECT query that takes one constraint and one column name $pdo = startDB(); $stmt = $pdo->prepare('SELECT '. $colName .' FROM '.$tableName.' WHERE '. $constraintCol .' = :constraint'); $values = [ @@ -63,30 +60,30 @@ function executeQuery($tableName, $colName, $constraintCol, $constraint) { return $stmt; } -function executeQueryWithoutConstraint($tableName, $colName) { +function executeQueryWithoutConstraint($tableName, $colName) { //execute a SELECT query with no constraint and one column name $pdo = startDB(); $stmt = $pdo->prepare('SELECT'.$colName.'FROM '.$tableName); $stmt->execute(); return $stmt; } -function getFirstMatch($tableName, $colName, $constraintCol, $constraint){ +function getFirstMatch($tableName, $colName, $constraintCol, $constraint){ //return the first match of an executeQuery return executeQuery($tableName, $colName, $constraintCol, $constraint)->fetch(); } -function getEveryMatch($tableName, $colName, $constraintCol, $constraint){ +function getEveryMatch($tableName, $colName, $constraintCol, $constraint){ //return every match of an executeQuery return executeQuery($tableName, $colName, $constraintCol, $constraint)->fetchAll(); } -function executeAllQuery($tableName, $constraintCol, $constraint) { +function executeAllQuery($tableName, $constraintCol, $constraint) { //execute a SELECT query with one constraint and all columns return executeQuery($tableName, '*', $constraintCol, $constraint); } -function getEveryAllMatches($tableName, $constraintCol, $constraint) { +function getEveryAllMatches($tableName, $constraintCol, $constraint) { //return every match of an executeALlQuery return executeAllQuery($tableName, $constraintCol, $constraint)->fetchAll(); } -function getFirstAllMatches($tableName, $constraintCol, $constraint) { +function getFirstAllMatches($tableName, $constraintCol, $constraint) { //return the first match of an executeAllQuery return executeAllQuery($tableName, $constraintCol, $constraint)->fetch(); } diff --git a/public/account/addAuction.php b/public/account/addAuction.php index 32568c7..6666b60 100644 --- a/public/account/addAuction.php +++ b/public/account/addAuction.php @@ -3,8 +3,8 @@ session_start(); $pageTitle = 'iBuy - Add Auction'; $stylesheet = '../assets/ibuy.css'; -if (!isset($_SESSION['loggedin'])) { - echo ''; +if (!isset($_SESSION['loggedin'])) { //redirects if user is not logged in + echo ''; //redirect } require_once '../../functions.php'; @@ -21,8 +21,8 @@ $pageContent = '

Add auction

require '../../layout.php'; if (isset($_POST['submit'])) { - if(imageUpload($_POST['title'].$_POST['endDate'])) { - $user = getFirstAllMatches('users', 'user_id', $_SESSION['loggedin']); + if(imageUpload($_POST['title'].$_POST['endDate'])) { //if the image upload is successful add auction + $user = getFirstAllMatches('users', 'user_id', $_SESSION['loggedin']); //get the first match of an all column query $pdo = startDB(); $stmt = $pdo->prepare('INSERT INTO auction (title, description, endDate, categoryId, email, imgUrl) diff --git a/public/account/editAuction.php b/public/account/editAuction.php index 99a7962..254d26b 100644 --- a/public/account/editAuction.php +++ b/public/account/editAuction.php @@ -20,7 +20,7 @@ require '../../layout.php'; if(isset($_POST['submit'])) { $pdo = startDB(); - if(isset($_POST['delete'])) { + if(isset($_POST['delete'])) { //delete the auction if selected $stmt = $pdo->prepare('DELETE FROM auction WHERE listing_id = :listing_id'); $values = [ 'listing_id' => $listing['listing_id'] @@ -28,7 +28,7 @@ if(isset($_POST['submit'])) { $stmt->execute($values); echo ''; } - if(imageUpload($_POST['title'].$_POST['endDate'])) { + if(imageUpload($_POST['title'].$_POST['endDate'])) { //if image upload is successful update the auction $stmt = $pdo->prepare('UPDATE auction SET title = :title, categoryId = :categoryId, endDate = :endDate, description = :description, imgUrl = :imgUrl WHERE listing_id = :listing_id'); $values = [ @@ -40,7 +40,7 @@ if(isset($_POST['submit'])) { 'imgUrl' => 'public/images/auctions/'.$_POST['title'].$_POST['endDate'] ]; $stmt->execute($values); - echo ''; + echo ''; //redirect } } diff --git a/public/account/login.php b/public/account/login.php index 3b437b7..4562ea7 100644 --- a/public/account/login.php +++ b/public/account/login.php @@ -15,14 +15,14 @@ require_once '../../functions.php'; $pdo = startDB(); if (isset($_POST['submit'])) { - $user = getFirstAllMatches('users', 'email', $_POST['email']); - if($user) { - if (password_verify($_POST['password'], $user['password'])) { + $user = getFirstAllMatches('users', 'email', $_POST['email']); //get the first match of an all column query + if($user) { //if the user exists + if (password_verify($_POST['password'], $user['password'])) { //if the entered and stored passwords match $_SESSION['loggedin'] = $user['user_id']; if ($user['admin'] === 'y') { $_SESSION['admin'] = 'y'; } - echo''; + echo''; //redirect } else { diff --git a/public/account/logout.php b/public/account/logout.php index 6d4ec10..4d8a6e0 100644 --- a/public/account/logout.php +++ b/public/account/logout.php @@ -1,6 +1,7 @@ window.location.href = "../index.php";'; +echo''; //redirect ?> \ No newline at end of file diff --git a/public/account/register.php b/public/account/register.php index 02b47d9..b783b8b 100644 --- a/public/account/register.php +++ b/public/account/register.php @@ -15,7 +15,7 @@ $pageContent = '

Already have an account?Click here to L require '../../layout.php'; if (isset($_POST['submit'])) { - addUser(false); + addUser(false); //adds the user to the db without admin privileges echo '

Successful account creation

'; } ?> \ No newline at end of file diff --git a/public/account/userReviews.php b/public/account/userReviews.php index 6c92e27..7ae9288 100644 --- a/public/account/userReviews.php +++ b/public/account/userReviews.php @@ -3,7 +3,7 @@ $pageTitle = 'iBuy - User Reviews'; require_once '../../functions.php'; checkId(); -$user = getFirstAllMatches('users', 'user_id', $_GET['user_id']); +$user = getFirstAllMatches('users', 'user_id', $_GET['user_id']); //get the first match of an all column query $pageContent = '

'.$user['first_name'].$user['last_name'].'\'s Reviews

'; @@ -11,7 +11,7 @@ $stylesheet = '../assets/ibuy.css'; require '../../layout.php'; function populateList() { - $reviews = getEveryAllMatches('review', 'review_user', $_GET['user_id']); + $reviews = getEveryAllMatches('review', 'review_user', $_GET['user_id']); //get every match of an all column query $output = ''; foreach ($reviews as &$review) { diff --git a/public/admin/addAdmin.php b/public/admin/addAdmin.php index a7a2775..c907628 100644 --- a/public/admin/addAdmin.php +++ b/public/admin/addAdmin.php @@ -3,7 +3,7 @@ session_start(); $pageTitle ='iBuy - Add Admin'; $stylesheet = '../assets/ibuy.css'; require_once '../../functions.php'; -adminCheck(); +adminCheck(); //checks to see if user is logged in as an admin $pageContent = '

Add Admin

@@ -15,7 +15,7 @@ $pageContent = '

Add Admin

require '../../layout.php'; if (isset($_POST['submit'])) { - addUser(true); - echo ''; + addUser(true); //adds user to the db with admin privileges + echo ''; //redirect } ?> \ No newline at end of file diff --git a/public/admin/addCategory.php b/public/admin/addCategory.php index 873eec3..ec9000c 100644 --- a/public/admin/addCategory.php +++ b/public/admin/addCategory.php @@ -3,7 +3,7 @@ session_start(); $pageTitle ='iBuy - Add Category'; $stylesheet = '../assets/ibuy.css'; require_once '../../functions.php'; -adminCheck(); +adminCheck(); //checks to see if user is logged in as admin $pageContent = '

Add Category

@@ -19,6 +19,6 @@ if (isset($_POST['submit'])) { 'name' => $_POST['name'] ]; $stmt->execute($values); - echo ''; + echo ''; //redirect } ?> \ No newline at end of file diff --git a/public/admin/adminCategories.php b/public/admin/adminCategories.php index ad7d130..c1f66bc 100644 --- a/public/admin/adminCategories.php +++ b/public/admin/adminCategories.php @@ -3,7 +3,7 @@ session_start(); $pageTitle = 'iBuy - Admin'; $stylesheet = '../assets/ibuy.css'; require_once '../../functions.php'; -adminCheck(); +adminCheck(); //checks to see if user is logged in as admin $pageContent = '

Categories Add

'; @@ -11,7 +11,7 @@ require '../../layout.php'; function populateContent() { $output = ''; - $cats = fetchCats(); + $cats = fetchCats(); //get all categories foreach ($cats as &$cat) { $output .= '
  • '. $cat['name'] . ' edit delete
  • '; } diff --git a/public/admin/deleteAdmin.php b/public/admin/deleteAdmin.php index 1fc1418..851c0bc 100644 --- a/public/admin/deleteAdmin.php +++ b/public/admin/deleteAdmin.php @@ -3,7 +3,7 @@ session_start(); $pageTitle = 'iBuy - Delete Admin'; $stylesheet = '../assets/ibuy.css'; require_once '../../functions.php'; -adminCheck(); +adminCheck(); //checks to see if user is logged in as admin if (isset($_GET['admin_id'])) { $pdo = startDB(); @@ -12,9 +12,9 @@ if (isset($_GET['admin_id'])) { 'category_id' => $_GET['admin_id'] ]; $stmt->execute($values); - echo ''; + echo ''; //redirect } else { - echo ''; + echo ''; //redirect } ?> \ No newline at end of file diff --git a/public/admin/deleteCategory.php b/public/admin/deleteCategory.php index fd3d73b..d14e472 100644 --- a/public/admin/deleteCategory.php +++ b/public/admin/deleteCategory.php @@ -3,7 +3,7 @@ session_start(); $pageTitle = 'iBuy - Delete Category'; $stylesheet = '../assets/ibuy.css'; require_once '../../functions.php'; -adminCheck(); +adminCheck(); //checks to see if user is logged in as admin if (isset($_GET['category_id'])) { $pdo = startDB(); diff --git a/public/admin/editAdmin.php b/public/admin/editAdmin.php index 8b9d05e..ed9b4dc 100644 --- a/public/admin/editAdmin.php +++ b/public/admin/editAdmin.php @@ -3,8 +3,8 @@ session_start(); $pageTitle = ''; $stylesheet = '../assets/ibuy.css'; require_once '../../functions.php'; -$admin = getFirstAllMatches('users', 'user_id', $_GET['admin_id']); -adminCheck(); +$admin = getFirstAllMatches('users', 'user_id', $_GET['admin_id']); //gets the first match from an all column query +adminCheck(); //checks to see if user is logged in as admin $pageContent = '

    Edit Admin

    @@ -44,6 +44,6 @@ else if (isset($_POST['submit'])) { $stmt->execute($values); unset($_SESSION['admin_id']); - echo ''; + echo ''; //redirect } ?> \ No newline at end of file diff --git a/public/admin/editCategory.php b/public/admin/editCategory.php index 1841c1f..8d7896d 100644 --- a/public/admin/editCategory.php +++ b/public/admin/editCategory.php @@ -4,7 +4,7 @@ $pageTitle = ''; $stylesheet = '../assets/ibuy.css'; require_once '../../functions.php'; $cat = getFirstAllMatches('category', 'category_id', $_GET['category_id']); -adminCheck(); +adminCheck(); //checks to see if user is logged in as admin $pageContent = '

    Edit Category

    @@ -24,6 +24,6 @@ else if (isset($_POST['submit'])) { ]; $stmt->execute($values); unset($_SESSION['cat_id']); - echo ''; + echo ''; //redirect } ?> \ No newline at end of file