我试图抓住该项目的相应ID,同时从select标签中选择它 然后通过隐藏输入发布它,以便插入带有项目名称的另一个表。
换句话说,我想插入项目的名称和相应的ID:
<form action="interface.php" method="post">
Equipment: <select name="equipment">
<option value="0">Выберите оборудование</option>
<?php
$res = mysql_query('select `equipment_id`,`name`,`ip_adress` from `equipment`');
while($row = mysql_fetch_assoc($res)){
do
{
printf("<option>%s</option>",$row['name']);
$equipment_id = $row['equipment_id'];
while ($row=mysql_fetch_array($res));
}
?>
</select>
<input type="hidden" name="equipment_id" value="<?php echo $equipment_id;?>" />
Interface.php处理程序:
<?php
include("connect.php");
$val1 = $_POST['equipment'];
$val2 = $_POST['equipment_id'];
$val3 = $_POST['vlan'];
$val4 = $_POST['vlan_id'];
$val5 = $_POST['name'];
$val6 = $_POST['description'];
mysql_select_db("ADRESSA") or die("Could not select database");
$result_insert = mysql_query ("INSERT INTO `ADRESSA`.`interface` (`equipment`, `equipment_id`, `vlan`,`vlan_id`,`name`,`description`)
VALUES ('$val1','$val2','$val3','$val4','$val5','$val6');")
or die ("<b>Query failed:</b> " . mysql_error());
if ($result_insert== 'true')
{
echo "1 record added";
header('Location: http://192.168.0.1/find/1/show_interface.php');
}
else{echo "try again";
}
mysql_close();
?>
问题是只在第一次正确插入值,在下一次查询期间,它只是添加相同的值。
祝你好运!
答案 0 :(得分:0)
我建议表单端的输出变成这样:
<select id="selEquipmentId" name="equipment_id">
<option value="0">Выберите оборудование</option>
<option value="1">Some Equipment Name (with the ID set in the value attribute)</option>
<!--
... more options using the same patern
-->
</select>
<!-- this hidden input element should contain the equipment name of the selected option -->
<input type="hidden" id="selEquipment" name="equipment" value="Выберите оборудование" />
然后使用一些javascript和JQuery magic在同一页面上包含以下脚本。如果你还没有include JQuery,也是如此。
<script>
$('#selEquipmentId').on('change', function() {
$('#selEquipment').val($('#selEqiupmentId option:selected').text());
});
</script>
最后一个提示:使用一个循环来创建选项。现在你在while循环中使用了“do while”-loop(据我所知,while循环只执行一次)。这只会造成混乱。