php - 给数据查询

标签 php mysql

我正在创建一个网页,用于查找和列出特定区域的医生。

我的两个 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);
?>

现在我想要一个 mysql 查询,它采用 city_nname 和 doctor_name(来自搜索框),然后给出属于该城市的医生列表。

最佳答案

请添加注释行“//添加此行”和修改行注释“//修改此行”

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>

后端搜索.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);
?>

关于php - 给数据查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41789518/

相关文章:

php - undefined variable : error while running the raw sql query

php - 替换关联数组中的值?

php - 在 32 位系统中对 64 位整数使用位操作(无 php_gpm 扩展)

php - 允许忽略 PHP 日期中的天或月

mysql - 列出所有产品并统计未下单的男性用户、女性用户

php - mySQLI - mysqli_real_escape_string 问题

php - 作为类名称部分无效的 PHP 关键字列表

mysql - 主键冲突

php - AJAX 发布不工作,数据似乎没有通过

mysql - 使用带有计数的 MYSQL Distinct?