gpt4 book ai didi

php - 使用多个 html 字段搜索 mysql

转载 作者:行者123 更新时间:2023-11-30 00:26:56 25 4
gpt4 key购买 nike

如何使用 html、php 和 mysql 添加多个字段来搜索数据库?

这是 HTML 代码。我想添加更多字段选项来搜索数据库中的名字、年龄、性别。示例:搜索[名字] AND/OR [年龄] AND/OR [性别]

<h2>Search</h2> 
<form name="search" action="searchresults.php" method="POST">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="firstName">First Name</option>
<Option VALUE="lastName">Last Name</option>
<Option VALUE="email">email</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

这是 PHP。

数据库连接

$find = strtoupper($find); 
$find = strip_tags($find);
$find = trim ($find);


$find=$_POST['find'];
$field=$_POST['field'];

$data="SELECT firstName, lastName, email, userphoto, age FROM contactInfo WHERE upper($field) LIKE '%$find%'";

$result = mysql_query($data);
$count=mysql_numrows($result);

echo '<br><br>';

if($count > 0){

echo"<table border=0>";

//get images and names in two arrays
$firstName= $row["firstName"];
$lastName= $row["lastName"];
$email= $row["email"];
$userphoto= $row["userphoto"];
$age= $row["age"];

$age = array();
$userphoto = array();
$firstName = array();
$lastName = array();

while ($row = mysql_fetch_array($result))
{
$userphoto[] = "<img src='images/".$row['userphoto']."' height='200' width='175'>";
$firstName[] = $row['firstName'];
$lastName[] = $row['lastName'];
$age[] = $row['age'];
$email[] = $row['email'];
}

while(!empty($userphoto))
{
//output images
foreach(array($userphoto, $firstName, $lastName, $age, $email) as $items)
{
echo "<tr>";
foreach($items as $key=>$item)
{
echo "<td><font size =\"3\" >$item</td>";
//output only four of them
if($key==4)
{
break;
}
}
echo "</tr>";
}
//remove the first five images from $images because they're already printed
$userphoto = array_slice($userphoto, 5);
$firstName = array_slice($firstName, 5);
$lastName= array_slice($lastName, 5);
$email = array_slice($email, 5);
$age = array_slice($age, 5);
}
echo"</table>";

最佳答案

我会将查询分为两部分:

首先创建一个静态部分:

$query = "从 contactInfo WHERE 中选择名字、姓氏、电子邮件、用户照片、年龄"

然后计算动态部分:

  • 对于用户根据和/或选项选择的每个字段:

$query = $query 。 $nameField . (“和” || “或”) 。 $valueField

关于php - 使用多个 html 字段搜索 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22802123/

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