Whenever I try to run sell.php
, the code will neither add to the balance nor add the transaction to the history table. There's nothing in the code that seems to be the cause of it. Why is this happening? I formatted the tables correctly.
sell.php
<?php
//config
require("../includes/config.php");
//If user arrived via "GET"
if ($_SERVER["REQUEST_METHOD"] == "GET")
{
//render form
render("sell_form.php", ["title" => "Sell"]);
}
//else if user arrived via "POST"
else if ($_SERVER["REQUEST_METHOD"] == "POST")
{
//Gather info
$info = query("SELECT shares FROM `stocks` WHERE id = ? AND symbol = ?", $_SESSION["id"], strtoupper($_POST["symbol"]));
$price = lookup(strtoupper($_POST["symbol"]));
$addcash = $price["price"] * floatval($info["shares"]);
//If user owns no shares of this stock
if ($info == [])
{
apologize("You don't own any shares of this stock.");
}
//Else, sell shares
else
{
query("DELETE FROM `stocks` WHERE id = ? AND symbol = ?", $_SESSION["id"], strtoupper($_POST["symbol"]));
query("UPDATE users SET cash = cash + ? WHERE id = ?", $addcash, $_SESSION["id"]);
query("INSERT INTO `history` (id, action, symbol, shares, price, date) VALUES(?, \"Sell\", ?, ?, ?, CURRENT_TIMESTAMP)", $_SESSION["id"], strtoupper($_POST["symbol"]), $_POST["shares"], $price["price"]);
//Redirect back
redirect("index.php");
}
}
?>