I have been working at trying to fix an issue I have been having with a variable. I keep getting an error for an undefined index for value
on line 49:
// update cash upon selling stock
query("UPDATE users SET cash = cash + ? WHERE id = ?", $positions["value"], $_SESSION["id"]);
I have tried many different phrases to try and pass the variable, but none seem to work. Any ideas? Here is the relevant chunk of my code:
... other code ...
// if user reached page via POST
else if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// make sure symbol is not empty
if (empty($_POST["stock2sell"]))
{
apologize("Please enter a stock symbol.");
}
else
{
// get users stocks and stock symbols
$rows = query("SELECT shares FROM userPortfolio WHERE id = ? AND symbol = ?", $_SESSION["id"], $_POST["stock2sell"]);
// get values
$positions = [];
foreach($rows as $row)
{
$stock2sell = lookup($_POST["stock2sell"]);
if ($stock2sell !== false)
{
$positions = [
"symbol" => $stock2sell["symbol"],
"name" => $stock2sell["name"],
"price" => $stock2sell["price"],
"shares" => $row["shares"],
"value" => ($row["shares"] * $stock2sell["price"])
];
}
else
{
apologize("That stock option does not exist.");
}
}
// update cash upon selling stock
query("UPDATE users SET cash = cash + ? WHERE id = ?", $positions["value"], $_SESSION["id"]);
// delete stock from users table
query("DELETE FROM userPortfolio WHERE id = ? AND symbol = ?", $_SESSION["id"], $_POST["stock2sell"]);
// get username, and cash
$ucrows = query("SELECT username, cash FROM users WHERE id = ?", $_SESSION["id"]);
// get values
$places = [];
foreach($ucrows as $ucrow)
{
$places[] = [
"usersname" => $ucrow["username"],
"cash" => $ucrow["cash"]
];
}
}
// render portfolio
render("sold.php", ["positions" => $positions, "places" => $places, "title" => "Sold", "id" => $_SESSION["id"]]);
}
?>