PHP MySQL 查询 jQuery 实时搜索框 - 无法根据输入类型从不同的列输出

PHP MySQL Query for jQuery Live Search Box - Unable to Output from Different Columns Based on Input Type

提问人:lostest 提问时间:4/29/2023 更新时间:4/29/2023 访问量:63

问:

我是 PHP/MySQL/jQuery 的新手,我被困在一个大型项目的这一部分。

对实时搜索框进行编码,我想根据用户输入返回数据库中已有的姓名或电话号码。我可以通过更改为“名称”或“电话”来让它只输出名称或电话号码,但我无法想出一个可行的解决方案来动态地执行任何一个 sumultaniouly。我已经研究了几个小时,使用 if/else 使用和/或,设置变量,以多种不同的方式重新处理查询,并进行了数小时的研究。任何建议都非常感谢!$variableis_numeric()is_nan()global

inventory.php:

<?php
// Session Start
session_start();
// If the user not logged in, redirect to the login page
if (!isset($_SESSION['loggedin'])) {
    header('Location: index.html');
    exit;
}

//Connect to database
require_once('connection.php');


if(isset($_REQUEST["term"])){

    $variable = "Name";            

    // Prepare a select statement
    $sql = "SELECT $variable FROM vendor WHERE $variable LIKE ?";

    if($stmt = mysqli_prepare($conn, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "s", $param_term);
        
        // Set parameters
        $param_term = $_REQUEST["term"] . '%';
        
        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            $vendresult = mysqli_stmt_get_result($stmt);
            
            // Check number of rows in the vendresult set
            if(mysqli_num_rows($vendresult) > 0){
                // Fetch vendresult rows as an associative array
                while($row = mysqli_fetch_array($vendresult, MYSQLI_ASSOC)){
                    echo "<p>" . $row["$variable"] . "</p>";
                }
            } else{
                echo "<p>No Match Found - Vendor Setup</p>";
            }
        } else{
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
        }
    }

    // Close statement
    mysqli_stmt_close($stmt);
}
 
// close connection
mysqli_close($conn);
?>

inventoryentry.php的HEAD部分:

            <script>
                $(document).ready(function(){
                    $('.search-box input[type="text"]').on("keyup input", function(){
                        /* Get input value on change */
                            var inputVal = $(this).val();
                            var vendresultDropdown = $(this).siblings(".vendresult");
                            if(inputVal.length){
                                $.get("backend-search.php", {term: inputVal}).done(function(data){
                                    // Display the returned data in browser
                                        vendresultDropdown.html(data);
                                });
                            } else{
                                vendresultDropdown.empty();
                            }
                    });
    
                    // Set search input value on click of vendresult item
                    $(document).on("click", ".vendresult p", function(){
                    $(this).parents(".search-box").find('input[type="text"]').val($(this).text());
                    $(this).parent(".vendresult").empty();
                    });
                });
            </script>

inventoryentry.php的BODY部分:

<div class="search-box"><input type="text" autocomplete="off" placeholder="Vendor Lookup" /><div class="vendresult"></div></div>

代码确实有效,但不是我想要的方式。

javascript php jquery mysql mysqli

评论


答:

1赞 Barmar 4/29/2023 #1

使用 查询两列。UNION

// Prepare a select statement
$sql = "SELECT name AS result FROM vendor WHERE name LIKE ?
        UNION
        SELECT phone AS result FROM vendor WHERE phone LIKE ?";
...
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ss", $param_term, $param_term);

获取结果时,请使用 而不是 。$row['result']$row[$variable]

评论

0赞 lostest 4/29/2023
我只是不确定用什么来代替$variableecho "<p>" . $row["$variable"] . "</p>";
0赞 Barmar 4/29/2023
这两个语句只是替换了代码中的类似语句。第一个替换您的线路,第二个替换您对 的呼叫。$sql =mysqli_stmt_bind_param
0赞 Barmar 4/29/2023
我已经更新了答案,以显示您在哪里使用它们。
0赞 lostest 4/29/2023
谢谢。我什至不知道存在!我仍然遇到的唯一问题是在那一行上,因为仅设置为从“名称”列返回数据,如果有人开始搜索电话号码,它将返回名称而不是电话号码。UNIONecho$variable
0赞 Barmar 4/29/2023
阅读我答案的最后一行。