我有sell.php和sell_form.php文件。他们在销售方面做得很好,但是如果他们为股票代码选择一个空值,我想尝试给用户一个错误信息。我已经实施了
'if(["blank"]){
apologize("You must select a stock symbol");
}'
我收到带有此if语句的错误消息,但我也无法出售所选股票。我不知道我在哪里弄错了。我也试过使用这个if语句
'if([empty($_POST["symbol"])){
apologize("You must select a stock symbol");
}'
这会将我重定向到主页,没有错误。任何帮助,将不胜感激。这是我的两个文件的代码。 sell.php
//configuration
require("../includes/config.php");
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if(empty($_POST["symbol"])){
apologize("You must select a stock symbol");
}
// lookup stock
$stock = lookup($_POST["symbol"]);
// lookup user's shares of stock being sold
$shares = query("SELECT shares FROM shares WHERE id = ? AND symbol = ?", $_SESSION["id"], $_POST["symbol"]);
// calculate total sale value (stock's price * shares)
$value = $stock["price"] * $shares[0]["shares"];
$date = date("Y-m-d, H:i:s");
// add the sale value to cash
query("UPDATE users SET cash = cash + ? WHERE id = ?", $value, $_SESSION["id"]);
// delete the stock from their portfolio
query("DELETE FROM shares WHERE id = ? AND symbol = ?", $_SESSION["id"], $_POST["symbol"]);
query("INSERT INTO history (id, symbol, soldOrBought, numberOfSharesBoughtOrSold, price, dateAndTime)
VALUES (?, ?, 'Sold', ?, ?, ?)", $_SESSION["id"], strtoupper($_POST["symbol"]), $shares[0]["shares"], $stock["price"], $date );
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
<ul class="nav nav-pills" >
<li >
<a href="index.php">Portfolio</a>
</li>
<li>
<a href="quote.php">Quote</a>
</li>
<li>
<a href="buy.php">Buy</a>
</li>
<li>
<a href="sell.php">Sell</a>
</li>
<li>
<a href="history.php">History</a>
</li>
<li>
<a href="deposit.php">Deposit Funds</a>
</li>
</ul>
<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["symbol"]?></option>
<? endforeach ?>
</select>
</div>
<div class="control-group">
<button type="submit" class="btn">Sell all shares</button>
</div>
</fieldset>
</form>
<div>
or <a href="logout.php">log out</a>
</div>
答案 0 :(得分:1)
您的选择列表可以包含“空白”选项,值为“空白”,因此检查空白不会对表单的常规帖子起作用。
您的支票应该是:
if(empty($_POST["symbol"]) || $_POST["symbol"]=="blank") {
apologize("You must select a stock symbol");
}
现在,当值完全丢失或值为“空白”时,它将显示此消息。另一种选择是改变选择列表以获得第一个选项,如
<select name="symbol">
<option value=""></option>
然后您只需检查if (empty($_POST["symbol"])) { }
答案 1 :(得分:0)
根据我的理解,函数apologize()正在打印输入文本,然后你必须在调用该函数后终止执行。这样你就可以得到错误信息。试试如下,
if(empty($_POST["symbol"])){
apologize("You must select a stock symbol");
die();
}
编辑:为了更好地理解,也可以发布apologize()定义。