0

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"]]);
}
?>

1 Answer 1

1

This means that $positions array don't have the key value in it.

Check isset($positions["value"]) before hand.

7
  • Right, I used isset() and it proves that it doesn't exist, but I declared it as one of the keys to the array. Yet, it turns out that none of the keys in my array are existing. This is my question I suppose. Where am I going wrong with my $positions associative array?
    – peachykeen
    Dec 18, 2015 at 1:39
  • You should execute those statements inside the block of if ($stock2sell !== false)
    – Aboalnaga
    Dec 18, 2015 at 1:41
  • They are inside that block...?
    – peachykeen
    Dec 18, 2015 at 1:49
  • query("UPDATE users ... is executed even if $stock2sell is false.
    – Aboalnaga
    Dec 18, 2015 at 1:52
  • Ah, I see. It is out of scope. You are awesome! Thank you so much for the insight :)
    – peachykeen
    Dec 18, 2015 at 2:01

You must log in to answer this question.

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