具有2个或更多字段的SQL搜索

时间:2016-04-27 13:29:58

标签: php mysql sql-server

创建了一个sql搜索查询,我创建了多个字段,如果使用if else条件它工作正常但是如果1和第2个字段是emty而第3个字段不是那么它因为OR关键字而不能正常工作请告诉我怎么做能纠正这个

<form method="POST" action="search.php?action=go">
            <li>
                <h3>Player</h3>
                <input type="text" class="form-control" placeholder="Dylan Scout" name="playername" value="<?php if(isset($_POST["playername"])) {echo $_POST["playername"];} ?>">
            </li>
            <li>
                <h3>Age</h3>
                <input type="text" class="form-control" placeholder="25" name="age" value="<?php if(isset($_POST["age"])) {echo $_POST["age"];} ?>">
            </li>
            <li>
                <h3>Country</h3>
                <input type="text" class="form-control" placeholder="Wallabies" name="country" value="<?php if(isset($_POST["country"])) {echo $_POST["country"];} ?>">
            </li>
            <li>
                <h3>Club</h3>
                <input type="text" class="form-control" placeholder="Eagle" name="club" value="<?php if(isset($_POST["club"])) {echo $_POST["club"];} ?>">
            </li>
            <li>
                <button type="submit" name="search">Search</button>
            </li>
        </form> 

这是我的sql php查询

<?php 
        if(isset($_GET["action"]) == 'go') {
            $stmt = "SELECT * FROM users WHERE";
            if($_POST["playername"]) {
                $stmt .= " OR fname LIKE '%".$_POST["playername"]."%' OR lname LIKE '%".$_POST["playername"]."%'";
            } 
            if($_POST["age"]) {
                $stmt .= " OR age LIKE '%".$_POST["age"]."%' ";
            }
            if($_POST["country"]) {
                $stmt .= " OR country LIKE '%".$_POST["country"]."%' ";
            }
            if($_POST["club"]) {
                $stmt .= " OR club LIKE '%".$_POST["club"]."%' ";
            }
        } else {
            $stmt = "SELECT * FROM users ";
        }   
        echo $stmt . "<br />";
        $sql = mysqli_query($connection, $stmt);
  ?>

请告诉我如何才能使其正常工作,就好像我在第3个字段上写字并将其他字段留空,然后它将变为asWHERE OR,这将成为明显错误的查询并且无法正常工作

谢谢

3 个答案:

答案 0 :(得分:4)

函数implode可以帮助您。

将它们添加到数组中并在之后连接它们。

<?php 
$array = array();
if (isset($_POST["playername"]))
    $array[] = "fname LIKE '%".$_POST["playername"]."%' OR lname LIKE '%".$_POST["playername"]."%";
if (isset($_POST["age"]))

   ...

$stmt = "SELECT * FROM users";
if (count($array) > 0)
    $stmt .= " WHERE " . implode(" OR ",$array);
$sql = mysqli_query($connection, $stmt);
?>

答案 1 :(得分:0)

试试这个。使用implode()即可实现此目标。

<?php 

    if(isset($_GET["action"]) == 'go') {

        $where = array();
        if($_POST["playername"]) {
            $where[] = " OR fname LIKE '%".$_POST["playername"]."%' OR lname LIKE '%".$_POST["playername"]."%'";
        } 
        if($_POST["age"]) {
            $where[] = " OR age LIKE '%".$_POST["age"]."%' ";
        }
        if($_POST["country"]) {
            $where[] = " OR country LIKE '%".$_POST["country"]."%' ";
        }
        if($_POST["club"]) {
            $where[] = " OR club LIKE '%".$_POST["club"]."%' ";
        }

        if(!empty($where))
        {
            $stmt = "SELECT * FROM users WHERE " . implode(" AND ", $where) ." ";
        }
        else
        {
            $stmt = "SELECT * FROM users ";
        }

    } else {
        $stmt = "SELECT * FROM users ";
    }   

    echo $stmt . "<br />";
    $sql = mysqli_query($connection, $stmt);
  ?>

答案 2 :(得分:0)

将where条件添加到数组中,然后使用implode function,例如:

<?php 
if(isset($_GET["action"]) == 'go') {
    $stmt = "SELECT * FROM users";
    if($_POST["playername"]) {
        $where[] = "fname LIKE '%".$_POST["playername"]."%' OR lname LIKE '%".$_POST["playername"]."%'";
    } 
    if($_POST["age"]) {
        $where[] = "age LIKE '%".$_POST["age"]."%' ";
    }
    if($_POST["country"]) {
        $where[] = "country LIKE '%".$_POST["country"]."%' ";
    }
    if($_POST["club"]) {
        $where[] = "club LIKE '%".$_POST["club"]."%' ";
    }

    if(count($where))
        $stmt .= " WHERE " . implode(" OR ", $where);

    echo $stmt . "<br />";
    $sql = mysqli_query($connection, $stmt);
?>