动态下拉列表可以工作,但如何获得价值?PHP

动态下拉列表可以工作,但如何获得价值?PHP,php,mysql,drop-down-menu,Php,Mysql,Drop Down Menu,嗨,我有以下代码: $dropdown = "<select name='test'>"; while($row = mysql_fetch_assoc($result)) { $dropdown .= "\r\n<option value='{$row['name']}'>{$row['name']}</option>"; } $dropdown .= "\r\n</select>"; echo $dropdown; <form n

嗨,我有以下代码:

$dropdown = "<select name='test'>";
while($row = mysql_fetch_assoc($result)) {
  $dropdown .= "\r\n<option value='{$row['name']}'>{$row['name']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;


<form name="input" action="databaseupdate.php" method="post">
Select the car you want to edit and fill in the form, followed by clicking the update button.
<br>
Carname: <input type="text" name="nameupdate">
Year build: <input type="text" name="yearupdate">
<input type="submit" value="Update">
</form>
$dropdown=”“;
while($row=mysql\u fetch\u assoc($result)){
$dropdown.=“\r\n{$row['name']}”;
}
$dropdown.=“\r\n”;
echo$下拉列表;
选择要编辑的汽车并填写表单,然后单击更新按钮。

卡纳姆: 建造年份:
还有这个(databaseupdate.php):

这样做:(只需在表单中添加下拉列表)


首先,在表单中包含您的下拉列表

第二,如果您需要下拉选择的ID,请在
$result
中从db中获取汽车名称的ID,然后执行以下操作:

$dropdown = "<select name='test'>";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "\r\n<option value='{$row['ID']}'>{$row['name']}</option>";
}
$dropdown .= "\r\n</select>";

<form name="input" action="databaseupdate.php" method="post">
Select the car you want to edit and fill in the form, followed by clicking the update button.
 <br>
echo $dropdown;
Carname: <input type="text" name="nameupdate">
Year build: <input type="text" name="yearupdate">
<input type="submit" value="Update">
</form>
$dropdown=”“;
while($row=mysql\u fetch\u assoc($result)){
$dropdown.=“\r\n{$row['name']}”;
}
$dropdown.=“\r\n”;
选择要编辑的汽车并填写表单,然后单击更新按钮。

echo$下拉列表; 卡纳姆: 建造年份:
完成了

$values包含选定的值

主要问题是您没有将select包含在表单标记中。另外,要从POST方法中获取值,您应该在PHP中使用$\u POST$\u REQUEST

若要检查$\u POST包含的内容,可以使用函数

var_dump($_POST);

你能把这个下拉列表包括在表格里吗?它说成功了,但它返回的是汽车的名字,而不是它附带的id。由于没有id为cars name的车辆,因此不会更新任何内容。这是因为您在选项值中提供了名称。在提交后更新了我的代码,使其具有id。我从数据库中选择了*,但它表示id未定义,但它会看到所有id行(对于每辆车,都有一个未定义的id),你知道如何解决这个问题吗?你能共享表结构和查询,以便快速检测错误吗?表cars有id、name和year$query=“SELECT*FROM cars”;
$dropdown = "<select name='test'>";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "\r\n<option value='{$row['ID']}'>{$row['name']}</option>";
}
$dropdown .= "\r\n</select>";

<form name="input" action="databaseupdate.php" method="post">
Select the car you want to edit and fill in the form, followed by clicking the update button.
 <br>
echo $dropdown;
Carname: <input type="text" name="nameupdate">
Year build: <input type="text" name="yearupdate">
<input type="submit" value="Update">
</form>
<form name="input" action="databaseupdate.php" method="post">
Select the car you want to edit and fill in the form, followed by clicking the update     button.
<br>
Carname: <input type="text" name="nameupdate">
Year build: <input type="text" name="yearupdate">
<select name='test'>
<?php while($row = mysql_fetch_assoc($result)) {
  echo "\r\n<option value='{$row['name']}'>{$row['name']}</option>";
}?>
</select>
<input type="submit" value="Update">
</form>
$values = $_POST['test'];
var_dump($_POST);