意外的变量问题

时间:2013-04-22 08:07:34

标签: php mysql

现在我得到的是我所投入的相同价值。所以它只是阅读第一个if语句,就是它并输出相同的价格。

$mysqli = new mysqli("localhost", "root", "", "insuredcars");
if ($_POST['formcar'] == '1' || $_POST['formage'] == '18' ||  $_POST['formNCD'] == '0' || $_POST['formPoints'] == '0' )
{
$query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '3'");
while($row = mysqli_fetch_assoc($query))
{
    echo "<tr>";
    echo "<td> The price for insurance will be " . $row['insuranceprice'] . "</td>";
    echo "</tr>";
}
echo "</table>";    
}
else if ($_POST['formcar'] == '6' || $_POST['formage'] == '18' ||  $_POST['formNCD'] ==      '0' || $_POST['formPoints'] == '0' )
{
$query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '2'");

echo "<tr>";
echo "<td> The price for insurance will be " . $row['insuranceprice'] . "</td>";
echo "</tr>";
echo "</table>";
}
  else if ($_POST['formcar'] == '1' || $_POST['formage'] == '19' ||  $_POST['formNCD'] ==     '0' || $_POST['formPoints'] == '0' )
{
  $query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '5'");

echo "<tr>";
echo "<td> The price for insurance will be " . $row['insuranceprice'] . "</td>";
echo "</tr>";
}
echo "</table>";

3 个答案:

答案 0 :(得分:2)

在PHP中,没有大括号{}的条件只会在其后执行单个行。你需要添加一些大括号。

<?php

    $mysqli = new mysqli("localhost", "root", "", "insuredcars");
    if ($_POST['formcar'] == '1' || $_POST['formage'] == '18' ||  $_POST['formNCD'] == '0'   || $_POST['formPoints'] == '0' ) {
        $query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '1'");
        while($row = mysqli_fetch_array($query)) {
            echo "<tr>";
            echo "<td> The price for insurance will be " . $row['insuranceprice'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    } elseif ($_POST['formcar'] == '6' || $_POST['formage'] == '18' ||  $_POST['formNCD'] == '0' || $_POST['formPoints'] == '0' ) {
        $query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '2'");
    }
    echo "<tr>";
    echo "<td> The price for insurance will be " . $row['insuranceprice'] . "</td>";
    echo "</tr>";
    echo "</table>";

?>

我不确定<tr>部分发生了什么,但是,这对你来说是一个好的开始。

答案 1 :(得分:0)

if声明没有括号...试试这个:

if(cond1){

}else if(cond2){

}

答案 2 :(得分:0)

许多{失踪或不适合。我试图猜测他们的位置。请尝试下面更正的语法,看看它是否按预期工作。

if ($_POST['formcar'] == '1' || $_POST['formage'] == '18' ||  $_POST['formNCD'] == '0' || $_POST['formPoints'] == '0' )
{
    $query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '1'");
    while($row = mysqli_fetch_array($query))
    {
        echo "<tr>";
        echo "<td> The price for insurance will be " . $row['insuranceprice'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";    
}
else if ($_POST['formcar'] == '6' || $_POST['formage'] == '18' ||  $_POST['formNCD'] == '0' || $_POST['formPoints'] == '0' )
{
    $query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '2'");

    echo "<tr>";
    echo "<td> The price for insurance will be " . $row['insuranceprice'] . "</td>";
    echo "</tr>";
    echo "</table>";
}
else if ($_POST['formcar'] == '1' || $_POST['formage'] == '19' ||  $_POST['formNCD'] ==     '0' || $_POST['formPoints'] == '0' )
{
    $query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '5'");

    echo "<tr>";
    echo "<td> The price for insurance will be " . $row['insuranceprice'] . "</td>";
    echo "</tr>";
}
echo "</table>";

备注:

除括号的位置外,代码中还存在一些逻辑错误。例如:

  • 第一个if块应该被执行if ($_POST['formage'] == '18') ......很好......
  • ...但你的其他声明也应如此......这是没有意义的。

我的猜测是你的意思是&&而不是||,但这只是猜测。