我正在创建一个网页,找出并列出给定区域的医生。
我的两个php脚本如下所示。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Live Doctor Search.</title>
<style type="text/css">
body{
font-family: Arail, sans-serif;
}
/* Formatting search box */
.search-box{
width: 300px;
position: relative;
display: inline-block;
font-size: 14px;
margin: 0 50%;
}
.search-box input[type="text"]{
height: 32px;
padding: 5px 10px;
border: 1px solid #CCCCCC;
font-size: 14px;
}
.result{
position: absolute;
z-index: 999;
top: 100%;
left: 0;
}
.search-box input[type="text"], .result{
width: 100%;
box-sizing: border-box;
}
/* Formatting result items */
.result p{
margin: 0;
padding: 7px 10px;
border: 1px solid #CCCCCC;
border-top: none;
cursor: pointer;
}
.result p:hover{
background: #f2f2f2;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.search-box input[type="text"]').on("keyup input", function(){
/* Get input value on change */
var term = $(this).val();
var resultDropdown = $(this).siblings(".result");
if(term.length){
$.get("backend-search.php", {query: term}).done(function(data){
// Display the returned data in browser
resultDropdown.html(data);
});
} else{
resultDropdown.empty();
}
});
// Set search input value on click of result item
$(document).on("click", ".result p", function(){
$(this).parents(".search-box").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
});
});
</script>
</head>
<body>
Select Your City:
<select name="city_nname">
<option value="Udaipur">Udaipur</option>
<option value="Jaipur">Jaipur</option>
<option value="Jodhpur">Jodhpur</option>
<option value="Sikar">Sikar</option>
<option value="Surat">Surat</option>
<option value="Ahmedabad">Ahmedabad</option>
<option value="badoda">badoda</option>
<option value="Pune">Pune</option>
<option value="Bangalore">Bangalore</option>
</select>
<div class="search-box">
<input type="text" autocomplete="off" placeholder="Search doctor..." />
<button type="submit" class="btn">Search</button>
<div class="result"></div>
</div>
</body>
</html>
以上代码是我的search_form.php。现在我想列出属于city_nname的医生名称。
这是我的php后端代码
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "demo_db");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$query = mysqli_real_escape_string($link, $_REQUEST['query']);
if(isset($query)){
// Attempt select query execution
$sql = "SELECT * FROM doctors WHERE doctor_name LIKE '" . $query . "%'";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
echo "<p>" . $row['doctor_name'] . "</p>";
}
// Close result set
mysqli_free_result($result);
} else{
echo "<p>No matches found for <b>$query</b></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
// close connection
mysqli_close($link);
?>
现在我想要一个带有city_nname和doctor_name(来自搜索框)的mysql查询,然后给出属于那个城市的医生列表。
答案 0 :(得分:0)
SELECT * FROM doctors WHERE doctor_name LIKE 'doctor_name%' AND city_nname='city_nname'
怎么样?我无法解决你在制定解决方案时遇到的麻烦。
答案 1 :(得分:0)
请添加评论行&#34; //添加此行&#34;和修改由&#34; //修改此行&#34;
的注释行<强> search_form.php 强>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Live Doctor Search.</title>
<style type="text/css">
body{
font-family: Arail, sans-serif;
}
/* Formatting search box */
.search-box{
width: 300px;
position: relative;
display: inline-block;
font-size: 14px;
margin: 0 50%;
}
.search-box input[type="text"]{
height: 32px;
padding: 5px 10px;
border: 1px solid #CCCCCC;
font-size: 14px;
}
.result{
position: absolute;
z-index: 999;
top: 100%;
left: 0;
}
.search-box input[type="text"], .result{
width: 100%;
box-sizing: border-box;
}
/* Formatting result items */
.result p{
margin: 0;
padding: 7px 10px;
border: 1px solid #CCCCCC;
border-top: none;
cursor: pointer;
}
.result p:hover{
background: #f2f2f2;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.search-box input[type="text"]').on("keyup input", function(){
/* Get input value on change */
var term = $(this).val();
var city_nname = $("#city_nname").val(); // Add this line
var resultDropdown = $(this).siblings(".result");
if(term.length){
$.get("backend-search.php", {query: term, city:city_nname}).done(function(data){ // Modify this line
// Display the returned data in browser
resultDropdown.html(data);
});
} else{
resultDropdown.empty();
}
});
// Set search input value on click of result item
$(document).on("click", ".result p", function(){
$(this).parents(".search-box").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
});
});
</script>
</head>
<body>
Select Your City:
<select name="city_nname" id="city_nname"> <!-- Modify this line -->
<option value="Udaipur">Udaipur</option>
<option value="Jaipur">Jaipur</option>
<option value="Jodhpur">Jodhpur</option>
<option value="Sikar">Sikar</option>
<option value="Surat">Surat</option>
<option value="Ahmedabad">Ahmedabad</option>
<option value="badoda">badoda</option>
<option value="Pune">Pune</option>
<option value="Bangalore">Bangalore</option>
</select>
<div class="search-box">
<input type="text" autocomplete="off" placeholder="Search doctor..." />
<button type="submit" class="btn">Search</button>
<div class="result"></div>
</div>
</body>
</html>
<强>后端-search.php中强>
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "bbtest");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$query = mysqli_real_escape_string($link, $_REQUEST['query']);
$city = mysqli_real_escape_string($link, $_REQUEST['city']); // Add this line
if(isset($query)){
// Attempt select query execution
$sql = "SELECT * FROM doctors WHERE doctor_name LIKE '" . $query . "%' AND city_nname='".$city."'"; // modify the table column name "city_nname" as like yours
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
echo "<p>" . $row['doctor_name'] . "</p>";
}
// Close result set
mysqli_free_result($result);
} else{
echo "<p>No matches found for <b>$query</b></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
// close connection
mysqli_close($link);
?>