如何在表单查找期间将隐藏的 SQL ID 字段值与其他数据一起传递?

How can I pass a hidden SQL ID field value alongside other data during a form lookup?

我正在尽最大努力组装一个基本的 属性 管理应用程序,它允许用户签出承包商的密钥(当然也可以将它们签回)。我是一个相对缺乏经验的编码员,目前正在努力研究如何在查找过程中传递额外的 SQL ID 值。

用户当前可以在 'searchcontractor' 输入中搜索承包商名称和公司。后端-search.php 文件使用 Contractors 数据库中的 FirstName、LastName 和 Company 填充此输入,但我不知道如何将该记录的 ContractorID 值也传递到隐藏的输入 'contractorid'.

非常感谢任何指点,谢谢。

PHP

<?php
include "msbkeys.php";
$sql = "SELECT KeySets.KeySetID, KeySets.KeyDescription, Sites.SiteID, Sites.SiteName FROM Sites INNER JOIN KeySets ON Sites.SiteID=KeySets.SiteID WHERE KeySets.KeyAssignedTo IS NULL;";
$result = $db->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>Keyset ID</th><th>Site ID</th><th>Site name</th><th>Description</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
    $keysetidsubmit = $row['KeySetID'];
echo "<tr><td>".$row["KeySetID"]."</td><td>".$row["SiteID"]."</td><td> ".$row["SiteName"]."</td><td> ".$row["KeyDescription"]."</td><td> <form  method='post'><input type='hidden' name='keysetid' value=".$keysetidsubmit."><div class='search-box'>
        <input name='searchcontractor' type='text' autocomplete='off' placeholder='Search contractor...' />
        <div class='result'></div>
    </div><input type='hidden' name='contractorid' value="**how can I populate this with the ID of the contractor chosen in the searchcontractor input?**"> <input name='checkoutkey' type='submit' value='Check out key'></form>  </td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
mysqli_close($db); // Close connection
?>

JavaScript

<script>
$(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>

后端-search.php

<?php
include "msbkeys.php";
 
if(isset($_REQUEST["term"])){
    // Prepare a select statement
    $sql = "SELECT * FROM Contractors WHERE CONCAT (FirstName,' ',LastName) LIKE ?";
    
    if($stmt = mysqli_prepare($db, $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>" . $row["FirstName"]." ".$row["LastName"] ." (".$row["Company"].")</p>";
                }
            } else{
                echo "<p>No matches found</p>";
            }
        } else{
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($db);
        }
    }
     
    // Close statement
    mysqli_stmt_close($stmt);
}
 
// close connection
mysqli_close($db);
?>

在包含承包商名称的 <p> 中添加承包商 ID 作为属性。

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
    echo "<p data-contractorid='{$row["id"]}'>" . $row["FirstName"]." ".$row["LastName"] ." (".$row["Company"].")</p>";
}

$row["id"] 替换为承包商 ID 的实际列名称。

然后将其复制到隐藏的输入字段。

$(document).on("click", ".result p", function(){
    $(this).closest(".search-box").find('input[type="text"]').val($(this).text());
    $(this).closest("td").find('input[name=contractorid]').val($(this).data('contractorid'));
    $(this).parent(".result").empty();
});