updated pdo

This commit is contained in:
Joshua Perry 2022-11-20 13:20:58 +00:00
parent 3f234df89a
commit 726f927987
10 changed files with 23 additions and 44 deletions

View File

@ -1,10 +1,6 @@
<?php
function fetchCats() {
$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 category');
$stmt->execute();
$cats = $stmt->fetchAll();
@ -22,4 +18,13 @@ function adminCheck() {
echo'<script>window.location.href = "../index.php";</script>';
}
}
function startDB() {
$server = 'mysql';
$username = 'student';
$password = 'student';
$schema = 'assignment1';
$pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password);
return $pdo;
}
?>

View File

@ -49,7 +49,7 @@ require_once 'functions.php';
echo $pageContent;
?>
<footer>
<a style="text-decoration: none;" href="admin/adminCategories.php">admins</a><br>
<a style="text-decoration: none;" href="../admin/adminCategories.php">admins</a><br>
&copy; ibuy <?php echo date('Y')?>
</footer>
</main>

View File

@ -10,12 +10,9 @@ $pageContent = '<p>Don\'t have an account?<a href=\'register.php\'>Click here to
</form>';
$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');

View File

@ -1,10 +1,8 @@
<?php
require_once '../../functions.php';
function addUser() {
$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 users (first_name, last_name, email, password, admin)
VALUES (:first_name, :last_name, :email, :password, :admin)');

View File

@ -12,11 +12,7 @@ $pageContent = '<h1> Add Category</h1>
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 = [

View File

@ -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']

View File

@ -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'],

View File

@ -10,20 +10,15 @@ if (isset($_GET['pageHeading'])) {
else {
$pageHeading = 'Latest Listings';
}
require_once '../functions.php';
$pageContent = '<h1>'.$pageHeading.'</h1>
<ul class="productList">'.populateList($pageHeading).'</ul>';
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();

View File

@ -4,15 +4,11 @@ $pageContent = '<h1>Product Page</h1>
<article class="product">'. populateContent() .'</article>';
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 = [