无法从php中删除db中的行

时间:2013-03-16 17:56:04

标签: php html sql

我有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>

1 个答案:

答案 0 :(得分:5)

我们认为在sell_form.php,

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

应更改为

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

其他方面在sell.php

delete语句正在解释为 -

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

Thnaks