2014-09-10 32 views
0

[page1.php中]PHP/MySQL的,如果返回基于价值信息的声明选择

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

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

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

+1

请解释'似乎没有工作' - 你有错误信息,你会得到什么? – Steve 2014-09-10 21:40:36

+0

@ user574632请参阅编辑 – user4013633 2014-09-10 21:41:41

+0

将错误检查添加到您的'mysqli_query()'调用中。 – Barmar 2014-09-10 21:42:27

回答

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>"; 
    } 
} 
+0

完美谢谢! – user4013633 2014-09-10 21:53:23

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>"; 
} 
}