0

我正在关注 Luke Welling 和 Laura thompson 关于 PHP-MYSQL Web 开发的书,我在 html 中有以下代码:

<form action="processorder.php" method=post>
<table border=0>
<tr bgcolor=#cccccc>
<td width=150>Item</td>
<td width=15>Quantity</td>
</tr>
<tr>
<td>Tires</td>
<td align="center"><input type="text" name="tireqty" size="3"
maxlength="3"></td>
</tr>
<tr>
<td>Oil</td>
<td align="center"><input type="text" name="oilqty" size="3" maxlength="3"></td>
</tr>
<tr>
<td>Spark Plugs</td>
<td align="center"><input type="text" name="sparkqty" size="3"
maxlength="3"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit Order"></td>
</tr>
</table>
</form>
<tr>
<td>How did you find Bob's</td>
<td><select name="find">
<option value = "a">I'm a regular customer
<option value = "b">TV advertising
<option value = "c">Phone directory
<option value = "d">Word of mouth
</select>
</td>
</tr>

和 php 中的这段代码:

<html>
    <head>
    <title>Bob's Auto Parts - Order Results</title>
    </head>
    <body>
    <h1>Bob's Auto Parts</h1>
    <h2>Order Results
    <?php
    //create short variable names
    $tireqty = $_POST['tireqty'];
    $oilqty = $_POST['oilqty'];
    $sparkqty = $_POST['sparkqty'];
    echo '<p>Order processed at ';
    echo date('H:i, jS F');
    echo '</p>';
    echo '<p>Your order is as follows: </p>';
    echo "$tireqty tires<br />";
    echo "$oilqty bottles of oil<br />";
    echo "$sparkqty spark plugs<br />";
    $totalqty = 0;
    $totalqty = $tireqty + $oilqty + $sparkqty;
    echo 'Items ordered: '.$totalqty.'<br />';
    $totalamount = 0.00;
    define('TIREPRICE', 100);
    define('OILPRICE', 10);
    define('SPARKPRICE', 4);
    $totalamount = $tireqty * TIREPRICE
    + $oilqty * OILPRICE
    + $sparkqty * SPARKPRICE;
    echo 'Subtotal: $'.number_format($totalamount,3).'<br />';
    $taxrate = 0.10; // local sales tax is 10%
    $totalamount = $totalamount * (1 + $taxrate);
    echo 'Total including tax: $'.number_format($totalamount,2).'<br />'
    switch($find)
    {
    case "a":
    echo "<p>Regular customer.</p>";
    break;
    case "b" :
    echo "<p>Customer referred by TV advert.</p>";
    break;
    case "c" :
    echo "<p>Customer referred by phone directory.</p>";
    break;
    case "d" :
    echo "<p>Customer referred by word of mouth.</p>";
    break;
    default :
    echo "<p>We do not know how this customer found us.</p>";
    break;
    }

    ?>

</h2>
</body>
</html>

按下提交按钮时出现服务器错误。开关块导致问题。我正在使用 PhP 5.3.10。谁能指出我的问题?提前致谢。

4

3 回答 3

4

我认为您还没有初始化 $find 变量...尝试在切换案例之前将其添加到您的 .php 文件中...

  $find=$_POST['find']

是的,你也忘了(;)

于 2013-11-04T11:04:25.100 回答
3

你错过了一个“;” 就在开关块之前。

--------------------------------------------------------------------▼
echo 'Total including tax: $'.number_format($totalamount,2).'<br />';

然后它将始终匹配默认情况,因为 $find 未定义。

于 2013-11-04T11:04:32.507 回答
1

导致 500 错误(或白页)的原因是:

echo 'Total including tax: $'.number_format($totalamount,2).'<br />'

你错过了; 在最后。您的 Switch 无法正常工作的原因是您缺少将 POST 分配给变量。像这样的东西:

$find=$_POST['find']

尽管这是处理 POST 的一种非常不安全的方式,但您应该始终清理您的帖子。使用这样的东西(这也可以扩展)

$find = isset($_POST['find']) ? strip_tags(trim($_POST['find'])) : '';

基本上,如果 $_POST['find'] 有一个值(isset),那么做问号之后的事情。剥离和 HTML、JavaScript 标记并修剪它之前或之后的任何空格,然后将 $_POST['find'] 分配给 $find 变量。如果没有设置,则 : 之后会给它一个 '' 或空白值。

希望有帮助。杰夫

于 2013-11-04T11:41:26.880 回答