0

My script has a two bugs, which I've been unable to fix. So, I need your help. Here is the file:

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

    // check if form was submitted
    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
        // type of transaction - for tracking history
        $transaction = 'SELL';

        // validate submission
        if (empty($_POST["symbol"]))
        {
            apologize("Select a stock to sell.");
        }
        else if (!empty($_POST["symbol"]))
        {
            if ($stock = lookup($_POST["symbol"]))
            {
                // select database
                $shares = query("SELECT shares FROM portfolios WHERE id = ?", $_SESSION["id"], $_POST["symbol"]);

                $value = $stock["price"] * $shares[0]["shares"];

                // delete stock's data from database table
                query("DELETE FROM portfolios WHERE id = ? AND symbol = ?", $_SESSION["id"], $_POST["symbol"]);

                // update users' DB table
                query("UPDATE users SET cash = cash + ? WHERE id = ?", $value, $_SESSION["id"]);

                // insert transaction into DB
                query("INSERT INTO history (id, transaction, symbol, shares, price) VALUES (?, ?, ?, ?, ?)", $_SESSION["id"], $transaction, $_POST["symbol"], $shares[0]["shares"], $stock["price"]);
            }
        }
        // redirect to portfolio
        redirect("/");
    }
    else
    {
        // query portfolio DB table
        $rows = query("SELECT * FROM portfolios WHERE id = ?", $_SESSION["id"]);

        // create an array to store current user's stock symbols
        $stocks = [];

        // iterate through each of the current user's stocks
        foreach ($rows as $row)
        {
            // save each stock symbol
            $stock = $row["symbol"];

            // add each stock symbol to the new array
            $stocks[] = $stock;
        }

        // render the sell form
        render("sell_form.php", ["title" => "Sell Stock", "stocks" => $stocks]);
    }

?>

The code doesn't update the user's cash as intended, after sales are made. As follows is my code that I thought would have handled the issue:

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

Still on the script, the insert transaction into history DB table doesn't seem to work. The history is supposed to display transaction on tran-hist.php. This is what I thought would have handled the transaction insertion into the database:

query("INSERT INTO history (id, transaction, symbol, shares, price) VALUES (?, ?, ?, ?, ?)", $_SESSION["id"], $transaction, $_POST["symbol"], $shares[0]["shares"], $stock["price"]);
2
Contributors
2
Replies
26
Views
3 Years
Discussion Span
Last Post by mexabet
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.