【问题标题】:How to post the result of this dropdown into an sql table?如何将此下拉列表的结果发布到 sql 表中?
【发布时间】:2016-07-03 13:00:23
【问题描述】:
<?php   
    $dbc = mysqli_connect("localhost","root","","bce_db");  

连接

    $query = "SELECT assignee_code,assignee_name FROM assignees";

从数据库中获取数据

    $sql = mysqli_query($dbc,$query);

    if(mysqli_num_rows($sql)){

        $select = '<select name = "select">';//dropdown created

        while($rs = mysqli_fetch_array($sql)){
          $select.='<option value = "'.$rs['assignee_code'].'">'.$rs['assignee_code'].'</option>';
         }
    }
    $select.='</select>';//dropdown ended..

    //I used the $POST method by linking it to other php page, but it returns undefined variable
    echo $select;
 ?>

如何在表格中发布下拉列表的值?

【问题讨论】:

  • 您必须向我们展示您的代码的重要部分(处理表单的代码 - 可能还有 &lt;form.. 标签)
  • 从您的评论中:_'我使用 $POST 方法...'_。请注意,如果您尝试访问从表单发送(通过邮寄)的变量,正确的用法是 [$_POST](php.net/manual/en/reserved.variables.post.php)
  • 不相关,但如果mysqli_num_rows($sql) 为假,则$select 可能未定义,从而导致返回一个单独的&lt;/select&gt; 标记。

标签: php mysql mysqli


【解决方案1】:
$select = '<select name = "select" onchange="location = this.value;">';
while($rs = mysqli_fetch_array($sql)){

  $select.='<option value = "addtosql.php?id='.$rs['assignee_code'].'">'.$rs['assignee_code'].'</option>'; //you can change the filename addtosql into whatever you want
 }
 $select.='</select>';

addtosql.php

<?php
//Connect to db
$id= $_GET['id'];
// Insert $id into db
?>

【讨论】:

    猜你喜欢
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    • 2018-02-28
    • 2014-07-04
    相关资源
    最近更新 更多