使用PHP向SQL语句从HTML表单选择框传递多个值
我有一个带有选择框的表单,用户可以在其中从数据库中选择预先存在的字段:使用PHP向SQL语句从HTML表单选择框传递多个值,php,html,sql,select,mysqli,Php,Html,Sql,Select,Mysqli,我有一个带有选择框的表单,用户可以在其中从数据库中选择预先存在的字段: <form action='' method='POST' autocomplete='off' enctype="multipart/form-data"> <p><strong>Title and Description:</strong></p> <select name='uidtitle' class='chosen-select'>
<form action='' method='POST' autocomplete='off' enctype="multipart/form-data">
<p><strong>Title and Description:</strong></p>
<select name='uidtitle' class='chosen-select'>
<option value="0" ></option>
<?php
$result = $mysqli->query("SELECT uid, title, description FROM tblFacilityHrs") or die($mysqli->error);
while ($row = $result->fetch_array(MYSQLI_ASSOC)){
echo "<option value=\"1\">" . $row['title'] ." - " . $row['description'] . "</option>";
$uid = $row['uid'];
$title = $row['title'];
$desc = $row['description'];
}
?>
</select>
...
我不知道你为什么要把这三个值都寄回去。数据库键的存在是为了能够识别一个记录中的所有字段(仅给定一个字段),在本例中,我假设
uid
。单独传递该字段将允许您在执行所需操作之前选择回发中的其他字段
但是,可以使用隐藏的表单字段,尽管我不提倡这种方法
<select name='uidtitle' class='chosen-select'>
<option value="0" ></option>
<?php
$result = $mysqli->query("SELECT uid, title, description FROM tblFacilityHrs") or die($mysqli->error);
$cacheArray = array(); // Used to store the information to be used below
while ($row = $result->fetch_array(MYSQLI_ASSOC)){
echo "<option value=\"" . $row['uid'] . "\">" . $row['title'] ." - " . $row['description'] . "</option>";
$cacheArray[] = $row;
}
?>
</select>
<?php
foreach($cacheArray as $k => $v) {
echo '<input type = "hidden" name = "title-' . $v['uid'] . '" value = "' . $v['title'] . '">';
echo '<input type = "hidden" name = "description' . $v['uid'] . '" value = "' . $v['description'] . '">';
}
?>
我不知道你为什么要把这三个值都寄回去。数据库键的存在是为了能够识别一个记录中的所有字段(仅给定一个字段),在本例中,我假设
uid
。单独传递该字段将允许您在执行所需操作之前选择回发中的其他字段
但是,可以使用隐藏的表单字段,尽管我不提倡这种方法
<select name='uidtitle' class='chosen-select'>
<option value="0" ></option>
<?php
$result = $mysqli->query("SELECT uid, title, description FROM tblFacilityHrs") or die($mysqli->error);
$cacheArray = array(); // Used to store the information to be used below
while ($row = $result->fetch_array(MYSQLI_ASSOC)){
echo "<option value=\"" . $row['uid'] . "\">" . $row['title'] ." - " . $row['description'] . "</option>";
$cacheArray[] = $row;
}
?>
</select>
<?php
foreach($cacheArray as $k => $v) {
echo '<input type = "hidden" name = "title-' . $v['uid'] . '" value = "' . $v['title'] . '">';
echo '<input type = "hidden" name = "description' . $v['uid'] . '" value = "' . $v['description'] . '">';
}
?>
你指的是
uid
、title
和description
?你指的是uid
、title
和description
吗?你说得绝对正确,我没有想到这一点。我想我只需要UID,你说得对,我没想到。我想我只需要UID。
$_POST['title-'.$_POST['uidtitle']]
$_POST['description-'.$_POST['uidtitle']]