For some reason it is impossible for me to compare the cost of the buy-transaction with the user's cash balance in pset7. $value correctly comes up as a float, but $cash comes up as a string (even though it's defined as a decimal in my database), so I cannot compare them correctly. I tried casting $cash to a float, but that didn't help either. If someone could help me figure out why this is happenning, and how I can remedy this bug, I would massively appreciate it. My code is below:
<?php
// configuration
require("../includes/config.php");
if($_SERVER["REQUEST_METHOD"] == "GET")
{
render("buy_form.php", ["title" => "Buy"]);
}
else if($_SERVER["REQUEST_METHOD"] == "POST")
{
// error-checking #1
if(empty($_POST["symbol"]) || empty($_POST["shares"]))
{
apologize("You need to input a symbol and quantity of shares.");
}
// check that shares is a whole positive number
else if(preg_match("/^\d+$/", $_POST["shares"]) != TRUE || !is_numeric($_POST["shares"]))
{
apologize("You need to input a non-negative, numeric number of shares.");
}
// get number of shares and the stock
$stock = lookup($_POST["symbol"]);
if($stock == false)
{
apologize("Stock not found.");
}
// calculate value of transaction
$value = $stock["price"] * $_POST["shares"];
//ensure that user can afford transaction
$cash = CS50::query("SELECT cash FROM users WHERE id = ?", $_SESSION["id"]);
var_dump($value, $cash);
// insert stocks into user's portfolio
// update user's cash
}
?>