0

I'm done with pset7 but my sell.php never updates the portfolio table when I sell stocks. it only displays the drop-down menu correctly where it shows the stocks bought in the list, but when i choose a symbol and click sell, i get redirected to portfolio without any change in the stocks nor in the cash value and also doesn't show any transactions in the history table.

Here's my sell.php:

<?php

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

$symbols = query("SELECT * FROM portfolio WHERE id = ?", $_SESSION["id"]);
    $totals = [];
    foreach ($symbols as $symbol)
    {
        $totals[] = [
        "sellsymbol" => $symbol["symbol"], "sellshares" => $symbol["share"]]; 
    }

if ($_SERVER["REQUEST_METHOD"] !== "POST")
{
    render_logged("sell_form.php", ["title" => "Sell", "totals" => $totals]);
}

else if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    $s2 = lookup($_POST['symbols']);
    $price = ["price" => $s2["price"]];

    $s1 = query("SELECT * FROM portfolio WHERE id = ? AND symbol = ?", $_SESSION["id"], strtoupper($_POST['symbols']));
    $value[] = ["share" => $s1["share"]];

    $costs = $value["share"] * $price["price"];

    query("DELETE FROM portfolio WHERE id = ? AND symbol = ?", $_SESSION["id"], strtoupper($_POST['symbols']));

    query("INSERT INTO history (id, symbol, shares, transaction) VALUES(?, ?, ?, ?)",$_SESSION["id"], strtoupper($_POST['symbols']), $totals["share"], "SELL");  

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

    redirect("index.php");
}

?>

and here's my sell_form.php:

<form action="sell.php" method="post">
<fieldset>
    <div class="form-group">
        <select name="symbols" style="width: 90px; padding: 3px;">
            <option value="symbols">Symbols</value>
        <?php foreach ($totals as $total): ?>  
            <option value="symbols"><?= $total["sellsymbol"] ?></value>
        <?php endforeach ?>   
        </select>
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-default" value="sell">Sell</button>
    </div>
</fieldset>

can anyone point to me where the mistake can be?

My buy.php file works fine except that cash also doesn't change. but it adds stocks to portfolio and shows the transaction movements in history, unlike sell.php

Thanks a lot in advance!

1
  • Consider how you declare value and the return type of lookup(). – ronga Nov 20 '15 at 3:34
2

if you look at your sell_form.html, you will find that at this line

<option value="symbols"><?= $total["sellsymbol"] ?></value>

you have an attribute value set equal to "symbols" you're always passing the string "symbols" to the server side, so whenever you use $_POST["symbol"] on your server side, it's actually as if you're using the string "symbol" and not passing the symbol the user actually passed, so instead of value, use name=symbol, and the value passed will be whatever symbol the user provides, in such case the deletion and update will happen.

try this instead, and let me know how did it go!

<option name="symbols"><?= $total["sellsymbol"] ?></option>

happy coding!:)

You must log in to answer this question.

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