Search User ID Email Ful-6ren">
gpt4 book ai didi

php - 如何使用搜索方法使用选择标签选项在 PHP、MYSQL 中搜索 id、电子邮件、用户名?

转载 作者:行者123 更新时间:2023-11-29 09:51:48 24 4
gpt4 key购买 nike

您好,我正在尝试使用带有 ID、电子邮件和全名选项的选择标记来获取搜索方法。

我有这个用于搜索的 html 代码:

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<select class="searchoptionuser" name="Search" id="searchbyuser">
<option value="" disabled selected value>Search User</option>
<option value="ID">ID</option>
<option value="Email">Email</option>
<option value="Fullname">Fullname</option>
</select>
<input type="text" name="search" placeholder="Search..." id="search_text" class="searchtext">
<input type="submit" name="submit" value="Search">
</form>

我想在选择 ID 时过滤搜索,搜索将仅按 ID 以及电子邮件和全名进行搜索。

我在 PHP 中尝试过:

<?php
include "includes/connection.php";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

if(isset($_POST['Search']) == "ID")
{
// query code here from database
alert(1);

} else if(isset($_POST['Search']) == "Email") {
// query code here from database

} else if(isset($_POST['Search']) == "Fullname") {
// query code here from database
$output = "";
$sql = "SELECT * FROM user WHERE firstname LIKE '%".$_POST["search"]."%' or lastname LIKE '%".$_POST["search"]."%'";
$result = mysqli_query($connection, $sql);

if (mysqli_num_rows($result) > 0) {
$output .= "<tr><th>User ID</th><th>Email</th><th>Fullname</th><th>Phone Number</th></tr>";
// output data of each row
while($row = mysqli_fetch_array($result)) {
$output .= "<tr>
<td>" .$row["id"]. "</td><td>" .$row["email"]. "</td><td>" .$row["lastname"]. ", " .$row["firstname"]. "</td><td>" .$row["phonenumber"]. "</td>
</tr>";
}
echo $output;
} else {
echo "No account found.";
}
}
}

?>

但它似乎不起作用..这在 php 中可能吗?或者这个方法仅适用于 javascript 或 jquery。请帮助任何人。谢谢

最佳答案

您可以根据从 $_POST 获取的数据创建条件查询。

$query = "";

if($_POST['Search'] == "ID")
{
$query .= "AND firstname LIKE '%".$_POST["search"]."%' or lastname LIKE '%".$_POST["Search"]."%'"";
} else if($_POST['Search'] == "Email") {
$query .= "AND email LIKE '%".$_POST["Search"]."%'";
} else if($_POST['Search'] == "Fullname") {
$query .= "AND ID = ".$_POST["Search"];
}

$sql = "SELECT * FROM user WHERE 1=1 ".$query ;

请忽略语法错误(如果有),因为我尚未测试代码,但它会让您了解如何实现它。

关于php - 如何使用搜索方法使用选择标签选项在 PHP、MYSQL 中搜索 id、电子邮件、用户名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54651572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com