【发布时间】:2016-04-04 21:12:58
【问题描述】:
php 中的多个 MySQL 查询。我试图弄清楚如何使用单选按钮在数据库中搜索多个查询。我可以搜索 ID,但我不能使用单选按钮搜索公司、名字、姓氏。这就是我得到的。
//search from search box
if(isset($_POST['search'])){
$searchQ = $_POST['search'];
$searchQ = preg_replace("#[^0-9a-z]#i","",$searchQ); //??
//Search by ID
// mysqli_query need 2 parameters $Db and query
$queryID = mysqli_query($DB, "Select id, company,first_name, last_name from customers WHERE id like '$searchQ'") or die ("could not search by ID");
$countID = mysqli_num_rows($queryID);
//Search by company
$queryCompany = mysqli_query($DB, "Select id, company,first_name, last_name from customers WHERE company like '%$searchQ%'") or die ("could not search by company ");
$countCompany = mysqli_num_rows($queryCompany);
//Search by first name
$queryFname = mysqli_query($DB, "Select id, company,first_name, last_name from customers WHERE first_name like '%$searchQ%'") or die ("could not search by First name ");
$countFname = mysqli_num_rows($queryFname);
//search by last name
$queryLname = mysqli_query($DB, "Select id, company,first_name, last_name from customers WHERE last_name like '%$searchQ%'") or die ("could not search by Last name ");
$countLname = mysqli_num_rows($queryLname);
//search all
$queryAll = mysqli_query($DB, "Select id, company,first_name, last_name from customers") or die ("could not search by All ");
$countAll = mysqli_num_rows($queryAll);
if ($countID == 0) {
$output = 'There was no search!';
}
else {
while($row = mysqli_fetch_assoc($queryID)){
$id = $row['id'];
$com = $row['company'];
$fname = $row['first_name'];
$lname = $row['last_name'];
$output ='<div> '.$id.' '.$com.' '.$fname.' '.$lname.'</div>';
}
}
}
?>
<h1>Customer List</h1><br /><br /><br />
<h3>Search from criteria</h3>
<form action="search.php" method="post">
<input type="text" name="search" placeholder="search database" /><br />
<input type="radio" name="SearchCategory" value="Id" />Id<br />
<input type="radio" name="SearchCategory" value="company" />Company<br />
<input type="radio" name="SearchCategory" value="firstName" />First Name<br />
<input type="radio" name="SearchCategory" value="lastName" />Last Name<br />
<input type="submit" value="search" />
</form><br /><br />
<p></p>
<?php print($output);
【问题讨论】:
-
你没有
SearchCategoryPOST 数组。