PHP高级搜索错误

时间:2017-08-28 03:56:15

标签: php mysql search

我正在尝试将PHP高级搜索设置到我的网站。 代码php是:

$sql = "SELECT * FROM users WHERE";
foreach($_POST AS $key => $value) {
    if(count($_POST) > 0 && !empty($_POST[$key])) {
        $value = clean_data($value);
        $sql .= " $key LIKE '%{$value}%' OR ";
    }
    $sql = rtrim($sql,' OR ');
    $res = mysqli_query($connection,$sql) or die(mysqli_error($connection));
    $row = mysqli_fetch_assoc($res);
    echo "<pre>";
    print_r($row);
}

html代码是:

<form action="search.php" method="POST">
    <h3>
        Search for plate number
    </h3>
    <input type="text" name="name" placeholder="Enter name">
    <input type="text" name="email" placeholder="Enter email">
    <input type="text" name="car_model" placeholder="Enter car model">
    <input type="submit" name="submit">
</form>

如果我提供姓名和电子邮件作为搜索字词我得到了:

<pre>
Array
(
    [id] => 22
    [name] => fname lname
    [email] => whatever@hotmail.com
    [plate_num] => 775hgy
    [password] => 4194933072aab7975beb010542011ea1387d54b0
    [gender] => 
    [country] => 
    [gov] => 
    [cell_num] => 
    [username] => username
    [profile_pic_url] => 
    [car_model] => 
    [age] => 0
    [location] => 
    [job] => 
    [license_pic_url] => plate_images/497002597df3168ebd6e2813f29b53ea.jpg
    [android_key] => b0178e8f817cd6c9ec6b5a438633e634
    [creation_date] => 2017-08-19 22:46:33
    [modif_date] => 2017-08-19 22:46:33
    [is_online] => 0
    [num_friends] => 0
    [activation_code] => b23f8f7cea65f5c1d003165e8f1f5c61
    [account_stats] => 1
    [user_ip] => ::1
)
  

您的SQL语法有错误;查看与MySQL服务器版本对应的手册,以便在第1行的'email LIKE'%what_i _entered_for_email%'附近使用正确的语法

我尝试了所有提供的解决方案,但没有运气。

我真的不知道出了什么问题。有什么想法吗?

4 个答案:

答案 0 :(得分:2)

OR 的问题以及 LIKE 运算符缺少单引号。试试以下代码中的任何一个。

    $sql = "SELECT * FROM users WHERE";
    $i = 0; // loop count
    foreach($_POST AS $key => $value) {
        if(!empty($key)) {
        $value = clean_data($value);
        $sql .= ($i++ > 0 ? " OR " : "")." $key LIKE '%{$value}%' ";
        echo "<br />".$sql; // print sql query and debug

        $res = mysqli_query($connection,$sql) or die(mysqli_error($connection));
        while ($row = mysqli_fetch_assoc($res)) {
        echo "<pre>";
        print_r($row);
        }
    }
}

    $sql = "SELECT * FROM users WHERE";
    foreach($_POST AS $key => $value) {
        if(!empty($key)) {
        $value = clean_data($value);
        $sql .= " $key LIKE '%{$value}%' OR ";
        echo "<br />".rtrim($sql,' OR '); // print sql query and debug

        $res = mysqli_query($connection,rtrim($sql,' OR ')) or die(mysqli_error($connection));
        while ($row = mysqli_fetch_assoc($res)) {
        echo "<pre>";
        print_r($row);
        }
    }
}

答案 1 :(得分:0)

更新此

$sql .= " $key LIKE %".$value."% OR ";

答案 2 :(得分:0)

试试这个

<?php
$sql = "SELECT * FROM users WHERE";
if(count($_POST) > 0) {
    foreach($_POST AS $key => $value) {
    if(!empty($key)) {
     $value = clean_data($value);
     $sql .= " $key LIKE '%{$value}%' OR";
    }
  }
    $sql = trim($sql,"OR");
    $res = mysqli_query($connection,$sql) or die(mysqli_error($connection));
    while ($row = mysqli_fetch_assoc($res)) {
    echo "<pre>";
    print_r($row);
    }    

}
?>

<强>释

您应该在添加OR条件之前检查 $ _ POST 的计数,然后在foreach循环后运行查询,并为字符串示例添加引号 - LIKE '%{$value}%'

答案 3 :(得分:0)

<?php
$sql = "SELECT * FROM users WHERE";
foreach($_POST AS $key => $value) {
    if(!empty($key)) {
        $value = clean_data($value);
        $sql .= " $key LIKE '%".$value."%' OR";
    }
}
$sql = substr_replace( $sql, "", -2 );
$res = mysqli_query($connection,$sql) or die(mysqli_error($connection));
while ($row = mysqli_fetch_assoc($res)) {
    echo "<pre>";
    print_r($row);
}
?>