PHP MySQL Query for jQuery Live Search Box -无法根据输入类型从不同列输出

kognpnkq  于 2023-04-29  发布在  jQuery
关注(0)|答案(1)|浏览(93)

我对PHP/MySQL/jQuery相当陌生,我被困在一个大项目的这一部分。
编码一个现场搜索框,我想返回姓名或电话号码已经在数据库中的基础上用户输入。我可以通过将$variable更改为“Name”或“Phone”来让它只输出Name或Phone Numbers,但我一直无法想出一个工作的解决方案来动态地执行任何sumultaniouly。我已经研究了几个小时/故障排除if/else使用is_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的HEAD部分。php:

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

库存条目的正文部分。php:

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

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

k3bvogb1

k3bvogb11#

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]

相关问题