Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 15, 2023 03:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/cf4d23167d87642653126367067cfedd to your computer and use it in GitHub Desktop.
Save code-boxx/cf4d23167d87642653126367067cfedd to your computer and use it in GitHub Desktop.
PHP MYSQL Auction System

PHP MYSQL AUCTION SYSTEM

https://code-boxx.com/auction-bidding-php-mysql/

IMAGES

apple

NOTES

  1. Create a dummy database and import 1-bid.sql.
  2. Change the database settings in 2-bid-lib.php to your own.
  3. Access 3-bid.php in the browser.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-- (A) AUCTION ITEMS
CREATE TABLE `auction_items` (
`item_id` bigint(20) NOT NULL,
`item_name` varchar(255) NOT NULL,
`item_img` varchar(255) DEFAULT NULL,
`item_desc` text DEFAULT NULL,
`bid_end` datetime DEFAULT NULL,
`bid_min` float NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `auction_items`
ADD PRIMARY KEY (`item_id`);
ALTER TABLE `auction_items`
MODIFY `item_id` bigint(20) NOT NULL AUTO_INCREMENT;
-- (B) AUCTION BIDS
CREATE TABLE `auction_bids` (
`item_id` bigint(20) NOT NULL,
`user_id` bigint(20) NOT NULL,
`bid_amount` float NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `auction_bids`
ADD PRIMARY KEY (`item_id`,`user_id`),
ADD KEY `amount` (`bid_amount`);
-- (C) DUMMY TEST ITEM
INSERT INTO `auction_items`
(`item_name`, `item_img`, `item_desc`, `bid_end`, `bid_min`)
VALUES
('An Apple', 'apple.png', 'A very high quality apple, not half eaten.', NULL, '1.5');
<?php
class Bid {
// (A) CONSTRUCTOR - CONNECT TO DATABASE
private $pdo = null;
private $stmt = null;
public $error = "";
function __construct () {
$this->pdo = new PDO(
"mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET,
DB_USER, DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
}
// (B) DESTRUCTOR - CLOSE DATABASE CONNECTION
function __destruct () {
if ($this->stmt!==null) { $this->stmt = null; }
if ($this->pdo!==null) { $this->pdo = null; }
}
// (C) EXECUTE SQL QUERY
function query ($sql, $data=null) : void {
$this->stmt = $this->pdo->prepare($sql);
$this->stmt->execute($data);
}
// (D) GET ITEM
function getItem ($id) {
// (D1) GET ITEM
$this->query("SELECT * FROM `auction_items` WHERE `item_id`=?", [$id]);
$item = $this->stmt->fetch();
if (!is_array($item)) { return null; }
// (D2) LAST HIGHEST BID + MINIMUM BID AMOUNT
$this->query("SELECT MAX(`bid_amount`) FROM `auction_bids` WHERE `item_id`=?", [$id]);
$item["highest"] = $this->stmt->fetchColumn();
if (!is_numeric($item["highest"])) { $item["highest"] = 0; }
$item["min"] = $item["highest"] + $item["bid_min"];
// (D3) RETURN RESULT
return $item;
}
// (E) UPDATE BID AMOUNT
function setBid ($iid, $uid, $amount) {
// (E1) CHECK IF ITEM EXIST
$item = $this->getItem($iid);
if ($item===null) {
$this->error = "Invalid Item";
return false;
}
// (E2) CHECK IF BIDDING ENDED
if (isset($item["bid_end"]) && strtotime("now") >= strtotime($item["bid_end"])) {
$this->error = "Bidding has ended";
return false;
}
// (E3) CHECK MINIMUM BID AMOUNT
if ($amount < $item["min"]) {
$this->error = "Please bid at least " . $item["min"];
return false;
}
// (E4) UPDATE HIGHEST BID
$this->query(
"REPLACE INTO `auction_bids` (`item_id`, `user_id`, `bid_amount`) VALUES (?,?,?)",
[$iid, $uid, $amount]
);
return true;
}
}
// (F) DATABASE SETTINGS - CHANGE TO YOUR OWN!
define("DB_HOST", "localhost");
define("DB_NAME", "test");
define("DB_CHARSET", "utf8mb4");
define("DB_USER", "root");
define("DB_PASSWORD", "");
// (G) NEW TO-DO OBJECT
$_BID = new Bid();
<!DOCTYPE html>
<html>
<head>
<title>Bidding Page Demo</title>
<meta charset="utf-8">
<link rel="stylesheet" href="x-dummy.css">
</head>
<body>
<?php
// (A) LOAD LIBRARY
require "2-bid-lib.php";
$iid = 1; // fixed item id for this demo
$uid = 999; // fixed user id for this demo
// (B) UPDATE BID AMOUNT
if (isset($_POST["bid"])) {
$ok = $_BID->setBid($iid, $uid, $_POST["bid"]);
echo "<div class='note'>". ($ok ? "Bid saved" : $_BID->error) ."</div>";
}
// (C) GET ITEM & DISPLAY
$item = $_BID->getItem($iid); ?>
<form method="post" class="bidWrap">
<!-- (C1) ITEM DETAILS -->
<img class="bidImg" src="<?=$item["item_img"]?>">
<div class="bidDetails">
<div class="bidName"><?=$item["item_name"]?></div>
<div class="bidHigh">Highest Bid $<?=sprintf("%0.2f", $item["highest"])?></div>
<div class="bidDesc"><?=$item["item_desc"]?></div>
</div>
<!-- (C2) BID -->
<div class="bidAmt">
<input type="number" name="bid" min="<?=$item["min"]?>" step="<?=$item["bid_min"]?>" value="<?=$item["min"]?>">
<input type="submit" value="BID!">
</div>
</form>
</body>
</html>
/* (X) NOT IMPORTANT - COSMETICS */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
body { background: #222; }
.note, .bidWrap { width: 300px; }
.note {
padding: 15px;
margin-bottom: 10px;
border: 1px solid #edc5c5;
background: #ffe5e5;
}
.bidWrap { background: #f7f7f7; }
.bidImg {
display: block;
width: 100%;
}
.bidDetails, .bidAmt {
margin-top: 10px;
padding: 10px;
}
.bidName {
font-weight: 700;
font-size: 20px;
}
.bidDesc, .bidHigh { font-size: 14px; }
.bidDesc, .bidHigh, .bidAmt { margin-top: 10px; }
.bidDesc { color: #484848; }
.bidHigh {
font-weight: 700;
color: #f95353;
}
.bidAmt {
display: flex;
}
.bidAmt input[type=number] {
flex-grow: 1;
padding: 10px;
border: 1px solid #aaa;
}
.bidAmt input[type=submit] {
padding: 10px 20px;
color: #fff;
background: #4b7dc7;
border: 0;
cursor: pointer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment