【问题标题】:The values are not getting posted to the database这些值没有发布到数据库
【发布时间】: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


【解决方案1】:
<?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>

试试这样的..

【讨论】:

  • 你为什么在一个页面中多次包含'configs.php'
  • 不,他们没有任何错误,但仍然没有发布价值
  • ive 删除了多个声明
  • ok 从一开始就检查后数组值是否来了,如果值来了,然后去 sql 查询回显它并检查它是否正在打印,然后在 phpmyadmin 中运行它并检查 sql 是否有错误希望这对你有很大帮助..
  • 我发现了我正在回显的 id 值的错误,我不得不回显 seae 而不是 id,因为我从一个表中检索它并将其插入到另一个表中,而不是 id我宣布 seae 现在可以正常工作了。
猜你喜欢
  • 2017-02-14
  • 2023-04-02
  • 2016-04-25
  • 2013-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-25
相关资源
最近更新 更多