Php 意外变量问题

Php 意外变量问题,php,mysql,Php,Mysql,现在我得到的只是我投入的相同价值。所以它只是读取第一个if语句,就是它,然后输出相同的价格 $mysqli = new mysqli("localhost", "root", "", "insuredcars"); if ($_POST['formcar'] == '1' || $_POST['formage'] == '18' || $_POST['formNCD'] == '0' || $_POST['formPoints'] == '0' ) { $query = $mysqli->

现在我得到的只是我投入的相同价值。所以它只是读取第一个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>";
$mysqli=新的mysqli(“本地主机”、“根”、“保险车”);
如果($\u POST['formcar']='1'|$\u POST['formage']='18'|$\u POST['formNCD']='0'|$\u POST['formPoints']='0')
{
$query=$mysqli->query(“从保险中选择*,其中保险ID='3'”);
while($row=mysqli\u fetch\u assoc($query))
{
回声“;
echo“保险价格将为“$row['insuranceprice'””;
回声“;
}
回声“;
}
如果($|u POST['formcar']='6'|$|$| u POST['formage']='18'|$|$| u POST['formNCD']='0'|$| u POST['formPoints']='0')
{
$query=$mysqli->query(“从保险中选择*,其中保险ID='2'”);
回声“;
echo“保险价格将为“$row['insuranceprice'””;
回声“;
回声“;
}
如果($|u POST['formcar']='1'|$|$| u POST['formage']='19'|$|$| u POST['formNCD']='0'|$| u POST['formPoints']='0')
{
$query=$mysqli->query(“从保险中选择*,其中保险ID='5'”);
回声“;
echo“保险价格将为“$row['insuranceprice'””;
回声“;
}
回声“;

在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>";

?>

如果
语句没有方括号,则您可以使用
。。。试试这个:

if(cond1){

}else if(cond2){

}

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

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($\u POST['formcar']='1'|$\u POST['formage']='18'|$\u POST['formNCD']='0'|$\u POST['formPoints']='0')
{
$query=$mysqli->query(“从保险中选择*,其中保险ID='1'”);
while($row=mysqli\u fetch\u数组($query))
{
回声“;
echo“保险价格将为“$row['insuranceprice'””;
回声“;
}
回声“;
}
如果($|u POST['formcar']='6'|$|$| u POST['formage']='18'|$|$| u POST['formNCD']='0'|$| u POST['formPoints']='0')
{
$query=$mysqli->query(“从保险中选择*,其中保险ID='2'”);
回声“;
echo“保险价格将为“$row['insuranceprice'””;
回声“;
回声“;
}
如果($|u POST['formcar']='1'|$|$| u POST['formage']='19'|$|$| u POST['formNCD']='0'|$| u POST['formPoints']='0')
{
$query=$mysqli->query(“从保险中选择*,其中保险ID='5'”);
回声“;
echo“保险价格将为“$row['insuranceprice'””;
回声“;
}
回声“;
注意事项:

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

  • 第一个
    if
    块应执行
    if($\u POST['formage']==
    “18”)
    …很好
  • …但你的else声明也应该如此…这毫无意义

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

错误在哪里以及添加了什么括号,但仍然得到错误:解析错误:语法错误,在C:\xampp\htdocs\search.php第16行的意外“}”,考虑分离关注点-不要混合使用MySQL,PHP和HTML在同一个脚本上:您将得到无法维护和不可使用的代码。我已经将其更改为很棒的代码,但出于某种原因,我一直在获得相同的值。这就像它只是读取第一条if语句,就是这样。好吧,除了括号的位置,代码中还有一些逻辑错误。例如,第一个
if
块应该执行
if($\u POST['formage']='18')
…很好,但是您的else语句也应该执行,这没有意义。我猜你指的是
&&
而不是
|
,但那只是一个猜测。在继续编写代码之前,也许您应该了解更多关于一般控制结构和布尔运算符的知识。