【问题标题】:PHP - Dynamic Drop Down List and fetching/inserting the value to a different tablePHP - 动态下拉列表并将值提取/插入到不同的表中
【发布时间】:2013-04-07 04:51:12
【问题描述】:

需要帮助的人...

我创建了一个表单,用户可以在其中输入名字和姓氏,然后发送到数据库。

那么,

我创建了一个动态下拉列表,用户可以在其中选择职位或注册年份等项目。所选选项将与用户的名字一起存储到不同的表中

还有姓氏。

虽然我能够从我的数据库中填充下拉列表。如何获取价值

从下拉列表中将其与提交的表单一起插入到不同的表格中。

这是我的代码。请帮忙... tnx

<html>
<head>
<title>NEW EMPLOYEE</title>

<?php

    mysql_connect("localhost","root","");
    $db1 = "emp_db1";
    $t3 ="emp_table";
    mysql_query("create database $db1");
    mysql_select_db($db1);
    mysql_query("create table $t3(id int not null auto_increment primary key,fname varchar(50),lname varchar(50),mname varchar(50),post_name varchar(50)) engine = InnoDB");

?>

</head>

<BODY bgcolor ="pink">



<?php

    if(isset($_POST['submit'])){
    $fname =$_POST['fname'];
    $lname =$_POST['lname'];
    $mname =$_POST['mname'];
    $post_name =$_POST['post_name'];
    $submit=$_POST['submit'];

    error_reporting(E_ALL ^ E_NOTICE);

    mysql_query("INSERT INTO emp_table(fname,lname,mname,post_name) VALUES('$fname','$lname','$mname','$post_name')");

    }

?>



<form name="add_data" method="post" target="<?php $_SERVER['PHP_SELF']?>">

    Register New Employee <br/><br/>
    First Name:<input size = "20" type="text" name="fname"/>
    Middle Name:<input size = "20" type="text" name="mname"/>
    Last Name:<input size = "20" type="text" name="lname"/><br/>
    Job Position List:
        <select name='post_name'>
        <option value="">--- Select ---</option>
        <?php
            if (isset ($db1)&&$db1!=""){

            }
        ?>
        <?php
            $list=mysql_query("select * from post_table order by id asc");

            //Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\testA\index.php on line 56
            while($row_list=mysql_fetch_assoc($list)){
        ?>
                <option value="<?php echo $row_list['id']; ?>"<?php if($row_list['id']==$db1){echo "selected"; } ?>>
                                     <?php echo $row_list['post_name'];?>
                </option>
            <?php
            }
            ?>
        </select>

    <input type="submit" name="submit" value="REGISTER" />

   </form>
</body>

【问题讨论】:

  • post_table 甚至存在吗?
  • 是的 post_table 存在。它包含 post_id(主键)和 post_name。

标签: php drop-down-menu


【解决方案1】:

一篇文章就有很多问题和问题:)

  1. 在脚本开头创建一个新表似乎不是一个好主意。

  2. mysql_* 函数已弃用。查看警告:https://www.php.net/manual/en/function.mysql-query.php

  3. 如果您希望表单回发到相同的 URL,则不需要 &lt;form target="..."&gt; 属性。

  4. 对于实际问题:看起来您实际上已准备好所有代码。它只是需要一些改进。有什么不适合您的具体内容吗?

应该怎么做:

  • 插入数据后重定向到“成功”页面。除非您想留在提交路径上以允许另一个插入。但您至少应该插入一个带有“成功”消息的段落(例如在表格上方)。
  • 字段验证。
  • 一些错误处理。

另见问题:

【讨论】:

  • 好吧,您在开始时提到创建表是正确的,我打算将其删除以进行最终确定。好吧,对于下拉列表,我设法使其以某种方式工作,但问题不是将位置名称插入到 emp_table,而是插入了 post_id。如何将 post_table 中的 post_name(如“计算机程序员”)插入到 emp_table 的 post_name 列中?我对 php 还是很陌生。呵呵
  • 只需使用valueoption标签的value属性的位置名称:&lt;option value="&lt;?php echo htmlspecialchars($row_list['post_name']); ?&gt;"。但!使用来自emp_table的位置主键作为参考是正确的方法,保持它!当然,您应该将emp_table.post_name 列重命名为post_id。见What's the best practice for primary keys in tables?。如果对您有帮助,请不要忘记接受答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多