【问题标题】:SQL syntax error; check the manual that corresponds to your MariaDB serverSQL 语法错误;检查与您的 MariaDB 服务器相对应的手册
【发布时间】:2017-06-22 08:42:54
【问题描述】:

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

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

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

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

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

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

请帮助大家。

提前谢谢.d

<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>

【问题讨论】:

  • 您应该尝试打印您的最终查询以查看它的作用。我很确定你会得到你的 sql 查询的错误。一些建议: - 如果可以的话,使用 PDO 而不是 mysqli 函数 - 我更喜欢使用语法 if(isset($_GET["iso_3"])) 但这取决于你 - 你可以使用替代语法:php.net/manual/en/control-structures.alternative-syntax.php

标签: php mysql


【解决方案1】:

我认为问题出在这里:

$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%'  ";

【讨论】:

    【解决方案2】:

    正如 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 必须遵循其他条件这一事实。

    【讨论】:

    • 在 MySQL 上你也可以这样做:`SELECT * FROM country WHERE 1" :)
    【解决方案3】:

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

    $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);
    

    【讨论】:

      【解决方案4】:
      $qrystring = "SELECT * FROM country where "; 
      if($usearchprofile)
        $qrystring .= " name like '%$usearchprofile%' ";     
       if($usearchnicename) {
       if($usearchprofile){
         $qrystring .= " OR nicename like '%$usearchnicename%' "; 
        }else{
         $qrystring .= "nicename like '%$usearchnicename%' "; 
       }
      }
      if($usearchiso3) {
       if($usearchprofile || $usearchnicename){
       $qrystring .= " OR iso3 like '%$usearchiso3%' ";
       }else{
       $qrystring .= " iso3 like '%$usearchiso3%' ";
      }
      }
      

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

      【讨论】:

      • 嗨亚当赫尔,我已经尝试了所有答案,但只有你的答案是正确的。但无论如何也要感谢其他人。
      猜你喜欢
      • 2019-05-29
      • 2017-06-30
      • 1970-01-01
      • 2019-01-11
      • 2018-11-28
      • 1970-01-01
      • 2019-08-06
      • 2018-11-09
      相关资源
      最近更新 更多