0

I have been working on the sell page for Pset 7. I have it so when i sell it removes the proper stock from the portfolios database. But for some reason it is not updating the cash in the users database.

<?php require("../includes/config.php");

$temp_array = lookup($_POST['symbol']);
$price = ["Price Per Share" => $temp_array["Price Per Share"]];


$temp_array2 = CS50::query("SELECT shares FROM portfolios WHERE user_id = ? AND symbol = ?", $_SESSION["id"],strtoupper($_POST['symbol']));
$num_shares = ["shares" => $temp_array2["shares"]];

$total_cost = $num_shares["shares"] * $price["Price Per Share"];

CS50::query("DELETE FROM portfolios WHERE user_id = ? AND symbol = ?", $_SESSION["id"], strtoupper($_POST['symbol']));


CS50::query("UPDATE users SET cash = cash + ? WHERE id = ?", $total_cost , $_SESSION["id"]);

redirect("index.php");

?>

If I take "$total_cost" out of the UPDATE query and replace it with a fixed number it will work. But i dont know what I'm doing wrong in either the: $price, $num_shares, or $total_cost, lines, because something there is not working properly. If anyone could point me in the right direction I would appreciate it.

1 Answer 1

1

To get the price per share, try:

$price=$temp_array["price"];

and to get number of shares, remember that query returns an array, and you want the 1st (index=0) row. So try:

$numshares=$temp_array2[0]["shares"];

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .