我创建了一个包含下拉框的表单。在进行选择并提交值后,它们不会在数据库中更新。表单的代码是:
<form id="myForm" name="myForm" action='insert.php' method='post' >
<input type='hidden' name='st' value=0>
<table style="text-align:center; width:100%">
<tr><td style="text-align:right"><label>Select SE/AE:</label></td><td style="text-align:left">
<?php
include "configs.php"; // Database connection using PDO
//$sql="SELECT name,id FROM student";
$sql="SELECT DISTINCT seae FROM se_ae "; //order by name
/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
echo "<select name=seae value='select one'>SE/AE</option>"; // list box select command
foreach ($dbo->query($sql) as $row){//Array or records stored in $row
echo "<option value=$row[id]>$row[seae]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?> </td>
<tr><td style="text-align:right"><label>Select Brand:</label></td><td style="text-align:left">
<?php
include "configs.php"; // Database connection using PDO
//$sql="SELECT name,id FROM student";
$sql="SELECT DISTINCT brand FROM se_ae "; //order by name
/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
echo "<select name=brand value='select one'>Brand</option>"; // list box select command
foreach ($dbo->query($sql) as $row){//Array or records stored in $row
echo "<option value=$row[id]>$row[brand]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?> </td>
<tr><td style="text-align:right"><label>Select Territory:</label></td><td style="text-align:left">
<?php
include "configs.php"; // Database connection using PDO
//$sql="SELECT name,id FROM student";
$sql="SELECT DISTINCT territory FROM se_ae "; //order by name
/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
echo "<select name='territory' id='territory' value=''>"; // list box select command
foreach ($dbo->query($sql) as $row){//Array or records stored in $row
echo "<option value=$row[id]>$row[territory]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?> </td>
</form>
我尝试将值发布到数据库的代码或文件是:
<?php
$connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server
$db = mysql_select_db("kmcrg", $connection); // Selecting Database from Server// Establishing Connection with Server
if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL
$seae = $_POST['seae'];
$brand=$_POST['brand'];
$territory=$_POST['territory'];
$name=$_POST['name'];
$e_id=$_POST['number'];
$email = $_POST['email'];
$contact = $_POST['contact'];
if($name !=''||$email !=''){
//Insert Query of SQL
$query = mysql_query("insert into employees(seae,brand,territory,name,e_id,email,contact) values ('$seae','$brand','$territory','$name','$e_id', '$email', '$contact')");
header('location:list.php');
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
}
?>
表格中还有其他字段,如姓名,电子邮件等。它们正在更新,但不是组合框中的值。有人可以帮忙吗?
答案 0 :(得分:0)
<?php
include("configs.php");
$sql="SELECT DISTINCT `seae` FROM se_ae ";
?>
<select name="seae">
<?php foreach ($dbo->query($sql) as $row){?>
<option value="<?php echo $row[id];?>">
<?php echo $row[seae];?></option>
<?php
}?>
</select>
尝试这样的事情..