I hope I can get some help here (and hopefully this isn't too much of a duplicate of another thread). I've got some code here that I want to have checked to see why this weird thing is happening and what I can do to fix it.

Here's sell.php:



    $current_stock = CS50::query("SELECT symbol FROM portfolios WHERE user_id = ?", $_SESSION["id"]);

    render("selling_form.php", ["stock" => $current_stock, "title" => "Sell"]);


Here's selling_form.php (it's a view, of course):

<form action="sell_stock.php" method="post">
    <div class="form-group">
        <select class="form-control" name="symbol">
            <option disabled selected value="">Symbol</option>
            <?php foreach ($stock as $stock_number): ?>
                <option value="<?= $stock_number["symbol"] ?>"><?= $stock_number["symbol"] ?></option>
            <?php endforeach ?>
        <input type="submit" class="btn btn-default" value="Sell"/>

And here's sell_stock.php:



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

    if ($stock === false)
        apologize("That's an invalid symbol.");

    $shares = CS50::query("SELECT shares FROM portfolios WHERE user_id = ?", $_SESSION["id"]);

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

    $total_cash = $stock["price"] * $shares["shares"];

    $update = CS50::query("UPDATE users SET cash = cash + ?", $total_cash);

    render("index.php", ["stock" => $stock, "total" => $total_cash, "update" => $update, "title" => "Portfolio"]);


The bug seems to be that the "DELETE" and "UPDATE" queries are run no matter what whenever a user comes to that page. I'm not sure how to fix it so that it only happens when the "Sell" button is clicked.

I know about the "onclick" attribute, but I've only ever seen it used with JavaScript and I'm not sure if I can call a PHP function from a JavaScript script. If it can be done, then I have an idea as to how I could do this (I'll post it here if asked to).

For good measure, here're index.php and portfolio.php (in that order), just to see if the code for the total cash amount is also buggy (both the index/portfolio stuff for the total cash amount and the stuff that I have for it in sell):


    // configuration

    $positions = [];
    $rows = CS50::query("SELECT shares, symbol FROM portfolios WHERE user_id = ?", $_SESSION["id"]);
    $cash = CS50::query("SELECT cash FROM users WHERE id = ?", $_SESSION["id"]);
    foreach ($rows as $row)
        $stock = lookup($row["symbol"]);
        if ($stock !== false)
            $positions[] = [
                "name" => $stock["name"],
                "price" => $stock["price"],
                "shares" => $row["shares"],
                "symbol" => $row["symbol"],
                "total" => $total["total"]

    // render portfolio
    render("portfolio.php", ["positions" => $positions, "cash" => $cash, "title" => "Portfolio"]);


    <table class="table table-striped table-hover">
        <?php foreach ($positions as $position): ?>

                    <td><?= $position["symbol"] ?></td>
                    <td><?= $position["name"] ?></td>
                    <td><?= $position["shares"] ?></td>
                    <td>$<?= $position["price"] ?></td>
                    <td>$<?= $position["total"] ?></td>
                    <td colspan="4">CASH</td>
                    <td>$<?= $cash[0]["cash"] ?></td>

        <?php endforeach ?>

Edit: I've posted the latest code on Pastebin; here's the link for sell: Pastebin link for PHP and HTML code for sell in pset7.

And here's the link for the Pastebin page where my index.php and portfolio.php is, if anyone wants to see it: Pastebin link for PHP and HTML code for index and portfolio in pset7.

I have a "Notice: Undefined index: shares in /home/ubuntu/workspace/pset7/public/sell_stock.php on line 16" error right now that I want to try to fix.

The database gets updated for the user I want to sell a stock for (in this case it's President Skroob since I gave him some shares via phpMyAdmin) even despite the error messages for sell_stock.php. And when I look in my database, the entry for the user is completely gone from the portfolios table. What am I doing wrong (or is it supposed to happen that way, but I just need to fix the code so I don't get the error message?)?

1 Answer 1


in sell_stock.php when u query for $shares add symbol also as selecting criteria because a user can have multiple entries in portfolio with different symbols and you want to search shares for a particular symbol only.Your current code returns an array of shares of different symbols of a user.

  • I tried to make those changes and I also changed the call to render in sell_stock.php to pass the variables into portfolio.php instead of index.php. But I still have errors on sell_stock.php; 2 on sell_stock itself and two on portfolio.php. Of the former two, the first is "undefined index: shares" and the second is "undefined index: symbol". For the other two, it's "Undefined variable: positions" and "Invalid argument supplied for foreach()". I've updated the code on Pastebin, so please look there again. Link for sell: pastebin.com/rs6TBEJu. portfolio: pastebin.com/KXSxeESE Dec 21, 2016 at 11:40
  • Just made some more changes and added the positions array to sell_stock, but I'm still getting errors. Please help. And I'm sorry for any trouble I'm causing the people here. Dec 21, 2016 at 12:32

You must log in to answer this question.

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