Php 使用多个html字段搜索mysql
如何使用html、php和mysql向搜索数据库添加多个字段 下面是HTML代码。 我想添加更多的字段选项来搜索数据库的名字,年龄,性别。 示例:搜索Php 使用多个html字段搜索mysql,php,mysql,Php,Mysql,如何使用html、php和mysql向搜索数据库添加多个字段 下面是HTML代码。 我想添加更多的字段选项来搜索数据库的名字,年龄,性别。 示例:搜索[名字]和/或[年龄]和/或[性别] <h2>Search</h2> <form name="search" action="searchresults.php" method="POST"> Seach for: <input type="text" name="find" /> in &
[名字]
和/或[年龄]
和/或[性别]
<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>";
$find=strtoupper($find);
$find=带标签($find);
$find=修剪($find);
$find=$_POST['find'];
$field=$_POST['field'];
$data=“从contactInfo WHERE upper($field)中选择名字、姓氏、电子邮件、用户照片、年龄,如“%$find%”;
$result=mysql\u查询($data);
$count=mysql\u numrows($result);
回音“
”;
如果($count>0){
回声“;
//获取两个数组中的图像和名称
$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\u fetch\u数组($result))
{
$userphoto[]=“”;
$firstName[]=$row['firstName'];
$lastName[]=$row['lastName'];
$age[]=$row['age'];
$email[]=$row['email'];
}
而(!empty($userphoto))
{
//输出图像
foreach(数组($userphoto、$firstName、$lastName、$age、$email)作为$items)
{
回声“;
foreach($key=>$item的项目)
{
回显“$item”;
//只输出其中四个
如果($key==4)
{
打破
}
}
回声“;
}
//从$images中删除前五个图像,因为它们已打印
$userphoto=array\u slice($userphoto,5);
$firstName=array\u slice($firstName,5);
$lastName=array\u slice($lastName,5);
$email=array\u slice($email,5);
$age=array\u slice($age,5);
}
回声“;
更改您的查询。$data="SELECT firstName, lastName, email, userphoto, age FROM contactInfo WHERE `".$field."` LIKE '%".$find."%'";
我将把查询分为两部分: 首先创建一个静态零件:
$query=“从contactInfo中选择名字、姓氏、电子邮件、用户照片、年龄”
然后计算出dinamic部分:
- 对于每个字段,用户根据和/或选项选择:
$query=$query.$nameField.(“and”| |或“).$valueField