2

我有 2 页 sell.php 和 sell_form.php 。当用户从 sell_form.php 的下拉菜单中选择股票代码时,我在 sell.php 中有一个查询,应该从数据库中删除该股票代码,但我现在得到的只是一个空白页我点击出售按钮。我不确定我是否完全理解下拉菜单中的值是如何传递给 sell.php 表单的。这是我到目前为止的代码

这是我的 sell.php 文件

if ($_SERVER["REQUEST_METHOD"] == "POST") 
{



    // delete the stock from their portfolio 
query("DELETE FROM shares WHERE id = ? AND symbol = ?", $_SESSION["id"],$_POST["symbol"]);  

redirect("/");
}
else
{

$rows = query("SELECT * FROM shares WHERE id = ?", $_SESSION["id"]);

    //create array to store the shares
    $shares = [];

    //for each of the user info

    foreach($rows as $row){

        //lookup stock info
        $stock = lookup($row["symbol"]);
        if($stock !== false){

            $shares[] = [
                "name" => $stock["name"],
                "price" => $stock["price"],
                "shares" => $row["shares"],
                "symbol" => $row["symbol"],
                "total" => $row["shares"]*$stock["price"]
            ];

        }
    }

// render portfolio
render("sell_form.php", ["shares" => $shares, "title" => "Sell"] );
}

这是我的 sell_form.php

<form action="sell.php" method="post">
<fieldset>     
    <div class="control-group">

        <select name="symbol">
        <option value="blank"></option>

        <?php foreach ($shares as $symbol):?>
        <option value="symbol"><?= $symbol["symbol"]?></option>
         <? endforeach ?>
        </select>     

    </div>
    <div class="control-group">
        <button type="submit" class="btn">Sell all shares</button>
    </div>
</fieldset>
</form>
4

1 回答 1

5

我们认为在 sell_form.php 中,

<option value="symbol"><?= $symbol["symbol"]?></option>

应改为

<option value="<?= $symbol["symbol"]?>"><?= $symbol["symbol"]?></option>

否则在 sell.php 中

删除语句解释为 -

DELETE FROM shares WHERE id = <<Spefied User Id>> AND symbol = 'symbol';

纳克斯

于 2013-03-16T18:20:12.037 回答