SQL语法错误;检查与MariaDB服务器对应的手册

时间:2017-06-22 08:42:55

标签: php mysql

我想使用多个输入字段在我的数据库中搜索,但它给了我一个错误。

错误消息:“您的SQL语法出错;请查看与您的MariaDB服务器版本对应的手册,以便在第1行的''附近使用正确的语法

如您所见,我有3个输入字段可在相应列中搜索。 并非所有字段都是强制性的用户只能搜索1或2个字段。

方案; 1.当我在第一个字段中输入任何字符串时,它将在数据库中搜索。

  1. 当我在第一个和第二个字段中输入任何字符串时,它将再次搜索。

  2. 现在,我只在第二个字段中输入了任何字符串。然后错误来了。

  3. 请帮助伙计们。

    先谢谢你。

    <html>
    <body>
    <form name="form" action="index1.php" method="get">
    Profile Number: <input id="profile_number" name="profile_number">
    Nice Name:      <input id="nice_name" name="nice_name">
    ISO 3:          <input id="iso_3" name="iso_3">
    
    <input type="submit" name="Submit" value="Search" />
    </form>
    
    <?php
    
        echo @$_GET['profile_number'];
        echo @$_GET['nice_name'];
        echo @$_GET['iso_3'];
    
        $conn = mysqli_connect("localhost","root","","autocomplete");
    
        if (mysqli_connect_errno()) {
            echo "Failed to connect: " . mysqli_connect_error();
        }
    
    if(isset($_GET['Submit']))
    
    {
        echo'
    <table width=100% style="border: 1px solid #a8c562;" cellpadding=3><tr>
        <td bgcolor=#cadd99 align=center style="border: 1px solid #a8c562;">Profile Number</td>
        <td bgcolor=#cadd99 align=center style="border: 1px solid #a8c562;">Country Name</td>
        <td bgcolor=#cadd99 align=center style="border: 1px solid #a8c562;">ISO3</td>
    ';
    
        $usearchprofile = @$_GET['profile_number'];
        $usearchnicename = @$_GET['nice_name'];
        $usearchiso3 = @$_GET['iso_3'];
    
        $qrystring = "SELECT * FROM country where ";  
    
        if($usearchprofile)
            $qrystring .= " name like '%$usearchprofile%'  ";
    
        if($usearchnicename)
            $qrystring .= " OR nicename like '%$usearchnicename%'  ";
    
        if($usearchiso3)
            $qrystring .= " OR iso3 like '%$usearchiso3%'  ";
    
    
        $userarray = mysqli_query($conn, $qrystring) or die(mysqli_error($conn));
    
        while ($usrow = mysqli_fetch_array($userarray))
        {
            echo'
    <tr>
    <td style="border: 1px solid #a8c562;" align=center>'.$usrow['name'].'</td>
    <td style="border: 1px solid #a8c562;" width=10% align=center>'.$usrow['nicename'].'</td>
    <td style="border: 1px solid #a8c562;" width=10% align=center>'.$usrow['iso3'].'</td>
    </tr>
    ';
        }
    
        echo'</table>';
    }?>
        </body>
    </html>
    

4 个答案:

答案 0 :(得分:2)

我认为问题在于:

$qrystring = "SELECT * FROM country where ";  

    if($usearchprofile)
        $qrystring .= " name like '%$usearchprofile%'  ";

    if($usearchnicename)
        $qrystring .= " OR nicename like '%$usearchnicename%'  ";

    if($usearchiso3)
        $qrystring .= " OR iso3 like '%$usearchiso3%'  ";

在上面的查询中,如果

if($usearchprofile)
            $qrystring .= " name like '%$usearchprofile%'  ";

这个条件不满足你的查询是这样的:

SELECT * FROM country where OR LIKE ...

这是错误的。你可以尝试:

$qrystring = "SELECT * FROM country where 1 = 1";  // 1 = 1 will always true. Now all the below conditions are independent of each other  

        if($usearchprofile)
            $qrystring .= " and name like '%$usearchprofile%'  ";

        if($usearchnicename)
            $qrystring .= " OR nicename like '%$usearchnicename%'  ";

        if($usearchiso3)
            $qrystring .= " OR iso3 like '%$usearchiso3%'  ";

答案 1 :(得分:1)

正如Mayank所指出的那样,如果第一个条件没有得到满足就会出现问题,这是一个方法......

    $qrystring = "SELECT * FROM country where 1=1 ";  

    if($usearchprofile) {
        $qrystring .= " OR name like '%$usearchprofile%'  ";
    }

    if($usearchnicename) {
        $qrystring .= " OR nicename like '%$usearchnicename%'  ";
    }
    if($usearchiso3) {
        $qrystring .= " OR iso3 like '%$usearchiso3%'  ";
    }

1=1是一个虚拟条件,可以满足OR必须遵循其他条件的事实。

答案 2 :(得分:0)

如下所示更改您的条件,然后尝试使用不同字段的代码

$qrystring_arry = array();
if($usearchprofile)
        $qrystring_arry[] = " name like '%$usearchprofile%'  ";

    if($usearchnicename)
        $qrystring_arry[] = " nicename like '%$usearchnicename%'  ";

    if($usearchiso3)
        $qrystring_arry[] = " iso3 like '%$usearchiso3%'  ";

$qrystring .= implode(" OR ",$qrystring_arry);

答案 3 :(得分:0)

Login Response: {"error":false,"user":{"br_code":12,"mem_id":13,"username":"test","email":"test@yahoo.com","created_at":"2016-07-22 09:05:21"}}<br /><br />{"error":false,"sl_summ":{"sl_desc":"PA : Savings Account","tr_date":"2015-08-17","actual_balance":"483.67","available_balance":"483.67"}}{"error":false,"sl_summ":{"sl_desc":"PA : Savings - Cash Bond","tr_date":"2015-08-28","actual_balance":"10129.43","available_balance":"10129.43"}}

这将停止问题,因为它会检查是否只有一个或多个集合