Php / MySQL if语句根据所选值返回信息

时间:2014-09-10 21:38:12

标签: php mysql

[page1.php中]

<form action="result.php" method="POST" style='color:white;font-family: __custom;'>
    <input type="text" placeholder="Search.." name="search" /><br><br>
    <center>
        <select name="example">
        <option value="A">A</option>
        <option value="B">B</option>
        <option value="C">C</option>
        </select>
        <br><br>
    </center>
    &nbsp;<input type="submit" value="GO!" id="sub"/>
</form><br><br><br>

[result.php]

<?php
$searchq = $_POST['search'] // search textbox;
//Connect to mySQL and retrieve all contacts
$conErr = "Error connecting to server or database";
$con = mysqli_connect("host", "user", "password", "database");
// Check connection
if (mysqli_connect_errno()) {
    echo $connErr;
}
if ($_POST['example'] == 'C') {
    echo "You chose inCludes";
    $result = mysqli_query($con, "SELECT * FROM people WHERE name LIKE '%$searchq%' ORDER BY name ASC");
    while ($row = mysqli_fetch_array($result)) {
        echo "<hr>";
        echo "All information regarding Select Statement";
        echo "<br>";
    }
} else if ($_POST['example'] == 'A') {
    echo "You chose EndsWith";
    $result = mysqli_query($con, "SELECT * FROM people WHERE name LIKE '$searchq%' ORDER BY name ASC");
    while ($row = mysqli_fetch_array($result)) {
        echo "<hr>";
        echo "All information based on Select Statement";
        echo "<br>";
    }
} else {
    echo "Blah";
}
mysqli_close($con);
?>
</div>

我要做的是在page1.php上,用户搜索从下拉列表中选择A,B或C的关键字。基于A,B,C,用户转到result.php,然后根据查询给出信息。

上面的代码似乎不起作用[我根本没有得到任何结果] [空白]。请帮忙。

2 个答案:

答案 0 :(得分:0)

如果要搜索以字符串结尾的内容,则必须将%通配符放在开头:

} else if ($_POST['example'] == 'A') {
    echo "You chose EndsWith";
    $result = mysqli_query($con, "SELECT * FROM people WHERE name LIKE '%$searchq' ORDER BY name ASC");
    while ($row = mysqli_fetch_array($result)) {
        echo "<hr>";
        echo "All information based on Select Statement";
        echo "<br>";
    }
}

答案 1 :(得分:0)

您在LIKE子句

之后错过了%通配符
else if ($_POST['example'] == 'A') {
echo "You chose EndsWith";
$result = mysqli_query($con, "SELECT * FROM people WHERE name LIKE '%$searchq%' ORDER BY name ASC");
while ($row = mysqli_fetch_array($result)) {
    echo "<hr>";
    echo "All information based on Select Statement";
    echo "<br>";
}
}