【发布时间】:2015-03-27 06:35:40
【问题描述】:
我创建了一个包含下拉框的表单。在我做出选择并提交值后,它们不会在数据库中更新。表格的代码是:
<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>";
}
}
?>
表单中还有其他字段,如姓名、电子邮件等。它们正在更新,但组合框中的值没有更新。有人可以帮忙吗?
【问题讨论】:
-
var_dump($_POST);在文件中,插入数据。并向我们展示它包含的内容。 -
似乎问题出在 '$query = mysql_query("insert into employees(seae,brand,territory,name,e_id,email,contact) 值 ('$seae','$brand', '$territory','$name','$e_id','$email','$contact')");' .它应该是'$query = mysql_query("insert into employees(seae,brand,territory,name,e_id,email,contact) values ('.$seae.','.$brand.','.$territory.' ,'.$name.','.$e_id.', '.$email.', '.$contact.')");'我相信。
标签: php html database combobox sql-update