gpt4 book ai didi

php - php 中的多个 MySQL 查询。我试图弄清楚如何使用单选按钮在数据库中搜索多个查询

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

php.ini 中的多个 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);

最佳答案

这不是我的做法,但试图保持最接近给定的代码,可能的解决方案如下所示:

<?php
if (isset($_POST['search'])){
$searchQ = preg_replace("#[^0-9a-z]#i", "", $_POST['search']);

switch (strtolower($_POST['SearchCategory'])) {
case 'id':
$query = mysqli_query($DB, "Select id, company,first_name, last_name from customers WHERE id like '$searchQ'") or die ("could not search by ID");
break;
case 'company':
$query = mysqli_query($DB, "Select id, company,first_name, last_name from customers WHERE company like '%$searchQ%'") or die ("could not search by company ");
break;
case 'firstname':
$query = 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 ");
break;
case 'lastname':
$query = 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 ");
break;
case 'all':
default:
$query = mysqli_query($DB, "Select id, company,first_name, last_name from customers") or die ("could not search by All ");
}

$count = mysqli_num_rows($query);

if ($count == 0) {
$output = 'There was no search!';
}
else {
while ($row = mysqli_fetch_assoc($query)) {
$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="radio" name="SearchCategory" value="all" /> All<br>
<input type="submit" value="search" />
</form><br><br>


<?php print($output);

您可以在几个地方稍微简化一下,但这应该可行。

关于php - php 中的多个 MySQL 查询。我试图弄清楚如何使用单选按钮在数据库中搜索多个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36412960/

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