dongmu6578 2017-01-22 09:55
浏览 21
已采纳

提供数据以进行查询

I am creating a web page which Finding out and Listing the Doctors in a Given Area.

My both php scripts are as below.

<!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>

The above code is my search_form.php. now I want to list out the the doctors name which are belongs to city_nname only.

Here is my php back end code

<?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);
?>

Now I want a mysql query that takes city_nname and doctor_name(from search box) and then give the list of doctors which are belongs to that city.

  • 写回答

2条回答 默认 最新

  • dongsashe6006 2017-01-22 10:17
    关注

    Please add lines which are comment by "// Add this line" and Modify lines which are comment by "// Modify this line"

    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>
    

    backend-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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?