1

搜索页面:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP Live MySQL Database 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;
    }
    .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 inputVal = $(this).val();
        var resultDropdown = $(this).siblings(".result");
        if(inputVal.length){
            $.get("backend-search.php", {term: inputVal}).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>
    <div class="search-box">
        <input type="text" autocomplete="off" id="id" name="id" placeholder="Search Products or codes..." />
        <div class="result"></div>
    </div>
</body>
</html>

后端搜索页面:

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "username", " ", "root");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

if(isset($_POST['term'])){
    // Prepare a select statement
    $sql = "SELECT * FROM ProductCodes WHERE item_name LIKE ?";
    //$sql = "SELECT * FROM ProductCodes WHERE ('item_name' LIKE ?) OR ('id' LIKE ?)";


    if($stmt = mysqli_prepare($link, $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)){
            $result = mysqli_stmt_get_result($stmt);

            // Check number of rows in the result set
            if(mysqli_num_rows($result) > 0){
                // Fetch result rows as an associative array
                while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
                    echo "<p>BA" . $row["id"] . " " . $row["item_name"] . "</p>";
                }
            } else{
                echo "<p>No matches found</p>";
            }
        } else{
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
        }
    }
     //var_dump(mysqli_error($db_conx));
    // Close statement
    mysqli_stmt_close($stmt);
}

// close connection
mysqli_close($link);
?>

请放轻松-新手警报!我在上面设置了一个表(ProductCodes),其中包含两个列 - item_name 和 id。我需要进行搜索以检查两列,但我尝试替换

$sql = "SELECT * FROM ProductCodes WHERE item_name LIKE ?";

"$sql = "SELECT * FROM ProductCodes WHERE ('item_name' LIKE ?) OR ('id' LIKE ?)";

我遇到的另一个问题是我需要表单的值只是 id 字段,但我看不到如何分配它。感谢您提供任何帮助,我尝试了各种组合,但我只是在兜圈子。

4

3 回答 3

0

您可以在查询中使用两个参数,如下所示:

// prepare and bind
$stmt = $conn->prepare("SELECT * FROM ProductCodes WHERE id = ? OR item_name LIKE ? ");
$stmt->bind_param("sss", $ProductCodes,$ID);
$ProductCodes = '%' . $_REQUEST['term'] . '%';
$ID = '%' . $_REQUEST['id'] . '%';
于 2018-08-14T05:00:10.213 回答
0

好的,凯特,我想我理解你的问题。您想使用一个输入来搜索此查询的两个字段。

让我们从调整查询开始:

$sql = 'SELECT id, item_name FROM ProductCodes WHERE item_name LIKE ? OR id = ?';

然后我们将参数绑定到它,因为有两个问号(?)我们需要两个参数。

$stmt->bind_param('ss', $searchName, $searchId);

$searchName = '%' . $_REQUEST['term'] . '%';
$searchId = $_REQUEST['term'];

或使用与您相同的功能。

mysqli_stmt_bind_param($stmt, "ss", $searchName, $searchId);

$searchName = '%' . $_REQUEST['term'] . '%';
$searchId = $_REQUEST['term'];

对不起,混乱!固定考虑参考。

于 2018-08-13T19:35:23.787 回答
0

您可以像这样绑定参数:

$stmt->bind_param('sssd', $code, $language, $official, $percent);

取自这里: http: //php.net/manual/ro/mysqli-stmt.bind-param.php

在您的情况下,这将是:

$stmt->bind_param('si', $yourStringValue, $yourIntValue);

并且您将需要修复您的查询以不使用 a likeid这可能是数字:

"SELECT * FROM ProductCodes WHERE item_name LIKE ? or id = ?"
于 2018-08-13T19:31:04.480 回答