php switch case错误

时间:2013-11-04 11:00:04

标签: php html

我关注的是Luke Welling和Laura thompson关于PHP-MYSQL网站开发的书我在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。谁能指出我的问题?提前谢谢。

3 个答案:

答案 0 :(得分:4)

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

  $find=$_POST['find']

是的,你也忘了(;)

答案 1 :(得分:3)

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

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

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

答案 2 :(得分: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变量。之后:如果没有设置,将给它一个'或空白值。

希望有所帮助。 杰夫