0

The history table only inserts sell transaction and i don't know why because both insertions are identical

both use

query("INSERT INTO history (id, transaction, symbol, shares, price) VALUES (?, ?, ?, ?, ?)", $_SESSION["id"], $transaction, $_POST["symbol"], $shares[0]["shares"], $stock["price"] );

where $transaction gets SELL or BUY. And it's not like buy.php is broken, everything else in there works fine.

<?php
    else if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
        if (empty($_POST["symbol"]) || empty($_POST["shares"]))
            apologize("You must fill in all fields.");

        if (lookup($_POST["symbol"]) === false)
            apologize("Invalid symbol.");

        if (preg_match("/^\d+$/", $_POST["shares"]) == false)
            apologize("You must enter a whole, positive integer.");

        $transaction = 'BUY';

        // get quote
        $stock = lookup($_POST["symbol"]);

        // calculate total
        $value = $stock["price"] * $_POST["shares"];

        // query user's cash
        $cash = query("SELECT cash FROM users WHERE id = ?", $_SESSION["id"]);  

        // if user's cash < total cost (user can't afford purchase)
        if ($cash < $value)
        {
            // apologize
            apologize("Insufficeient funds.");
        }         

        {
            // capitalize symbol (works)
            $_POST["symbol"] = strtoupper($_POST["symbol"]);

            // add stock to their portfolio or update shares
            query("INSERT INTO shares (id, symbol, Shares) VALUES(?, ?, ?)  ON DUPLICATE KEY UPDATE shares = shares + VALUES(shares)", $_SESSION["id"], $_POST["symbol"], $_POST["shares"]);

            // subtract total price from cash
            query("UPDATE users SET cash = cash - ? WHERE id = ?", $value, $_SESSION["id"]);

            // update history
              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 if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
        $transaction = 'SELL';

        // get quote
        $stock = lookup($_POST["symbol"]);

        // get quantity
        $shares = query("SELECT shares FROM shares WHERE id = ? AND symbol = ?", $_SESSION["id"], $_POST["symbol"]);

        // multiply stock price by quantity
        $value = $stock["price"] * $shares[0]["shares"];

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

        // remove stocks from portfolio
        $query = query("DELETE FROM shares WHERE id = ? AND symbol = ?", $_SESSION["id"], $_POST["symbol"]);

        // update history
        query("INSERT INTO history (id, transaction, symbol, shares, price) VALUES (?, ?, ?, ?, ?)", $_SESSION["id"], $transaction, $_POST["symbol"], $shares[0]["shares"], $stock["price"] );

        // redirect to home
        redirect("/");
    }?>
0

i added an apologize("test"); after the query. there it showed me the error , i had forgotten to declare $shares in the buy file.

0

Take a look at where your php tag closes. Anything outside the php tag will not be executed. Also your two else if methods are identical, so only the first will ever be run. If you look a little closer you will see that it's not even formatted as php code by the site because it's outside the php tag. I helped a little with the code formatting to make this more distinguishable.

1
  • thank you, but the two else ifs are in different files, so they both run. and every instruction after the mysterious query is executed, this is what confuses me :( Aug 10 '15 at 12:41

You must log in to answer this question.

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