我想在选项值中显示 id 等于角色 ID 的数据。例如,角色 1 具有分类 1 和分类 2,在这种情况下,它仅在选项值中显示分类 1 和分类 2。我的问题是,如果我得到ID(它在输入中),那么我编写查询,选项值为空,但在文本字段中,ID 是好的。我将另一个 PHP 文件中的数据 id 获取到输入。如何解决这个问题以显示 id 等于输入值的分类?
index.php
<?php
$id = $_POST["RoleID"];
$testQuery = $conn->prepare("SELECT classification.class_id AS id, classification.name AS name
FROM classification
INNER JOIN role_classification ON classification.class_id = role_classification.class_id
WHERE role_classification.role_id = ?");
$testQuery->bind_param("i", $id);
$testQuery->execute();
$testResult = $testQuery->get_result();
$testQuery->close();
?>
<form method="post" id="insert_form">
<select name="classification[]" id="classification" multiple>
<?php
while($row = $testResult->fetch_array()) {
?>
<option value="<?php echo $row['id'];?>"><?php echo $row['name'];?></option>
<?php
}
?>
</select>
<input type="hidden" name="RoleID" id="RoleID" />
</form>
最佳答案
向您的输入添加一个值:
<input type="hidden" name="RoleID" id="RoleID" value=" "/>
可能是<input type="hidden" name="RoleID" id="RoleID" value="<?php echo $row['id'];?"/>
关于php - 如何从输入中获取值并在 PHP 查询中使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51697540/