added editAuction functionallity

This commit is contained in:
Joshua Perry 2022-11-20 14:44:18 +00:00
parent 05f96aa493
commit 5d26b0fada
4 changed files with 63 additions and 20 deletions

View File

@ -27,4 +27,29 @@ function startDB() {
$pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password); $pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password);
return $pdo; return $pdo;
} }
function checkListing() {
if (!isset($_GET['listing_id'])) {
echo '<script>window.location.href = "index.php";</script>';
}
}
function getListing() {
$pdo = startDB();
$stmt = $pdo->prepare('SELECT * FROM auction WHERE listing_id = :listing_id');
$values = [
'listing_id' => $_GET['listing_id']
];
$stmt->execute($values);
return $stmt->fetch();
}
function populateCatSelect() {
$cats = fetchCats();
$output = '';
foreach ($cats as &$cat) {
$output .= '<option value="'. $cat['category_id'] .'">'. $cat['name'] .'</option>';
}
return $output;
}
?> ?>

View File

@ -13,7 +13,7 @@ $pdo = startDB();
$pageContent = '<h1>Add auction</h1> $pageContent = '<h1>Add auction</h1>
<form action="addAuction.php" method="POST"> <form action="addAuction.php" method="POST">
<label>Title</label> <input name="title" type="text" placeholder="Auction Title"/> <label>Title</label> <input name="title" type="text" placeholder="Auction Title"/>
<label>Category</label> <select name="category" style="width:420px; margin-bottom: 10px;">'. populateCats() .'</select> <label>Category</label> <select name="category" style="width:420px; margin-bottom: 10px;">'. populateCatSelect() .'</select>
<label>End Date</label> <input name="endDate" type="date"/> <label>End Date</label> <input name="endDate" type="date"/>
<label>Description</label> <textarea name="description" style="width: 438px; height: 249px;" placeholder="description"></textarea> <label>Description</label> <textarea name="description" style="width: 438px; height: 249px;" placeholder="description"></textarea>
<input name="submit" type="submit" value="Submit" style="margin-top: 10px;"/> <input name="submit" type="submit" value="Submit" style="margin-top: 10px;"/>
@ -42,11 +42,5 @@ if (isset($_POST['submit'])) {
echo '<p>Successful Post</p>'; echo '<p>Successful Post</p>';
} }
function populateCats() {
$cats = fetchCats(); ?>
$output = '';
foreach ($cats as &$cat) {
$output .= '<option value="'. $cat['category_id'] .'">'. $cat['name'] .'</option>';
}
return $output;
}

View File

@ -0,0 +1,32 @@
<?php
$pageTitle = 'iBuy - Edit Auction';
$stylesheet = '../assets/ibuy.css';
require_once '../../functions.php';
checkListing();
$pdo = startDB();
$listing = getListing();
$pageContent = '<h1>Edit Auction</h1>
<form action="editAuction.php?listing_id='.$listing['listing_id'].'" method="POST">
<label>Title</label> <input name="title" type="text" placeholder="'. $listing['title'] .'"/>
<label>Category</label> <select name="category" style="width:420px; margin-bottom: 10px;">'. populateCatSelect() .'</select>
<label>End Date</label> <input name="endDate" type="date"/>
<label>Description</label> <textarea name="description" style="width: 438px; height: 249px;" placeholder="'. $listing['description'] .'"></textarea>
<input name="submit" type="submit" value="Submit" style="margin-top: 10px;"/>
</form>';
require '../../layout.php';
if(isset($_POST['submit'])) {
$stmt = $pdo->prepare('UPDATE auction SET title = :title, categoryId = :categoryId, endDate = :endDate, description = :description');
$values = [
'title' => $_POST['title'],
'categoryId' => intval($_POST['category']),
'endDate' => $_POST['endDate'],
'description' => $_POST['description']
];
$stmt->execute($values);
echo '<script>window.location.href = "../listing.php?listing_id='.$listing['listing_id'].'";</script>';
}
?>

View File

@ -7,19 +7,11 @@ $pageContent = '<h1>Product Page</h1>
require '../layout.php'; require '../layout.php';
if (!isset($_GET['listing_id'])) { checkListing();
echo '<script>window.location.href = "index.php";</script>';
}
function populateContent() { function populateContent() {
$pdo = startDB(); $pdo = startDB();
$listing = getListing();
$stmt = $pdo->prepare('SELECT * FROM auction WHERE listing_id= :listing_id');
$values = [
'listing_id' => $_GET['listing_id']
];
$stmt->execute($values);
$listing = $stmt->fetch();
$stmt = $pdo->prepare('SELECT * FROM category WHERE category_id = :category_id'); $stmt = $pdo->prepare('SELECT * FROM category WHERE category_id = :category_id');
$values = [ $values = [
@ -48,7 +40,7 @@ function populateContent() {
<h3>'. $category['name'] .'</h3> <h3>'. $category['name'] .'</h3>
<p>Auction created by <a href="#">'. $user['first_name'].$user['last_name'] .'</a></p> <p>Auction created by <a href="#">'. $user['first_name'].$user['last_name'] .'</a></p>
<p class="price">Current bid: '. $bid['MAX(amount)'] .'</p> <p class="price">Current bid: '. $bid['MAX(amount)'] .'</p>
<time>Time left:'. round((strtotime($listing['endDate']) - strtotime(date('Y-m-d H:i:s')))/60,1 ) .' Minutes</time> <time>Time left:'. round((strtotime($listing['endDate']) - strtotime(date('Y-m-d H:i:s')))/60/60,1 ) .' Hours</time>
<form action="#" class="bid"> <form action="#" class="bid">
<input type="text" name="bid" placeholder="Enter bid amount" /> <input type="text" name="bid" placeholder="Enter bid amount" />
<input type="submit" value="Place bid" /> <input type="submit" value="Place bid" />