Skip to content

Commit

Permalink
Insert to basket
Browse files Browse the repository at this point in the history
  • Loading branch information
vrana committed Aug 11, 2012
1 parent beafd01 commit 56ca53b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
29 changes: 26 additions & 3 deletions category.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,36 @@
if (!$row) {
pageNotFound();
}

$error = "";
if ($_POST["id"]) {
if (!checkCSRF()) {
$error = "<p>Invalid CSRF token.</p>\n";
} else if ($_POST["amount"] <= 0) {
$error = "<p>Invalid amount.</p>\n";
} else {
$_SESSION["basket"][$_POST["id"]] += $_POST["amount"];
redirect("cat/$row[url]/");
}
}

$products = query("SELECT id, name, about, price FROM products WHERE categories_id = %d AND visible = 1", $row["id"]);

htmlHead($row["name"], $row["url"]);
echo $error;

foreach ($products as $product) {
echo "<h3>" . h($product["name"]) . "</h3>\n";
echo "<p>" . h($product["about"]) . "</p>\n";
if ($product["price"] !== null) {
echo "<p><b>$product[price]</b></p>\n";
}
?>
<form action="" method="post">
<p>
<b><?=$product["price"]?></b>
<input type="hidden" name="csrf" value="<?=$_SESSION["csrf"]?>">
<input type="hidden" name="id" value="<?=$product["id"]?>">
<input name="amount" value="1" size="3">
<input type="submit" value="Buy">
</p>
</form>
<?php
}
1 change: 1 addition & 0 deletions functions.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function query($sql /*, ... */) {
function pageNotFound() {
header("HTTP/1.1 404 Not Found");
htmlHead("Page Not Found");
exit;
}

function redirect($path) {
Expand Down

0 comments on commit 56ca53b

Please sign in to comment.