dongxia527680 2011-09-15 12:08
浏览 34
已采纳

mysql数据库使用已在html / php中输入的数据制作下拉菜单

I created a database with 3 tables being spusername, splocation, sprecord. spusername has id, splocation_id, lastname, firstname. I want to be able to have a drop down menu that has pulled id, lastname, firstname from the database, and within the pulldown it only shows a list of all the names being lastname,firstname. then once I select a person I have another drop down that has types of training in it. then when I hit submit it will generate a record in another table with the persons id and training record. so when I do a search it will pull up the user and the training records for that person.... I have already created a submit page in a .php that sends lastname, firstname, splocation_id for new users and I think I can create a search that does what I want it to, but I have never made a data entry doing a pulldown that has values generated from the database.

EDIT Code: With help from Vegard's coding I got this, and now it works great after a few trial and errors. Thank You!

Code:

<?php 
    if (isset($_REQUEST['Submit'])) { 
    $sql = "INSERT INTO $db_table(spusername_id,sptraining_id) values ('".mysql_real_escape_string(stripslashes($_REQUEST['spusername_id']))."','".mysql_real_escape_string(stripslashes($_REQUEST['sptraining_id']))."')";
    if($result = mysql_query($sql ,$db)) { 
    echo '<h1>Thank you</h1>Your information has been entered into the database<br><br>'; 
    } else { 
    echo "ERROR: ".mysql_error(); 
    } 
    } else { 
?> 
<h1>Add Training Information To Database</h1><hr> 
<br><br>
<form method="post" action=""> 
<select name="spusername_id">
    <option value="default">Select Employee</option>
<?php
    include("connectspusers.php"); /*file where you have stored your DB conn. settings*/
    $result = mysql_query('SELECT id, lastname, firstname FROM spusername ORDER BY lastname ASC') or die (mysql_error()); 

    while ($row = mysql_fetch_array($result)) {
        echo '<option value="' . $row['id'] . ' ' . $row['lastname'] . ' ' . $row['firstname'] . '">' . $row['lastname'] . ', ' . $row['firstname'] . '</option>';
    }
?>
</select>

<select name="sptraining_id">
    <option value="default">Select Training</option>
<?php
    include("connectsptraining.php"); /*file where you have stored your DB conn. settings*/
    $result = mysql_query('SELECT id, trainingtype, level FROM sptraining ORDER BY level ASC') or die (mysql_error()); 

    while ($row = mysql_fetch_array($result)) {
        echo '<option value="' . $row['id'] . ' ' . $row['trainingtype'] . ' ' . $row['level'] . '">' . $row['trainingtype'] . ' - ' . $row['level'] . '</option>';
    }
?>
</select>
<br><br>
<input type="submit" name="Submit" value="Submit"> 
</form> 
<?php 
} 
?> 
  • 写回答

4条回答 默认 最新

  • dqenv99518 2011-09-15 12:52
    关注

    Something like this?

    <select name="pulldown1">
        <option value="default">Choose an option</option>
        <?php
        include("connect.php"); /*file where you have stored your DB conn. settings*/
        $result = mysql_query('SELECT id, lastname, firstname FROM spusername ORDER BY firstname ASC') or die (mysql_error()); 
    
        while ($row = mysql_fetch_array($result)) {
        echo '<option value="' . htmlentities($row['id'], ENT_QUOTES) . ' ' . htmlentities($row['lastname'], ENT_QUOTES) . ' ' . htmlentities($row['firstname'], ENT_QUOTES) . '">' . htmlentities($row['lastname'], ENT_QUOTES) . ', ' . htmlentities($row['firstname'], ENT_QUOTES) . '</option>';
        }
        ?>
    </select>
    
    <select name="pulldown2">
        <option value="default">Choose and option</option>
        <?php
    
        $result = mysql_query('SELECT traingtype FROM trainingtable ORDER BY trainingname ASC') or die (mysql_error()); 
    
        while ($row = mysql_fetch_array($result)) {
            echo '<option value="' . $row['trainingtype'] . '">' . $row['trainingtype'] . '" "' . $row['lastname'] . '</option>';
        }
        ?>
    </select>
    

    This will result in two dropdown menus where the first dropdown lists the users last- and firstname separated by a comma+space and the second will list the different types of training. The ID filed is only sendt via the variable, but not displayed to the user.

    When pulling the values from the variable in pulldown1, just use explode:

    $userdetails = $_POST['pulldown1'];
    $values = explode(" " $userdetails);
    $ID = $values[0];
    $lastname = $values[1];
    $firstname = $values[2];
    

    Haven't tested the code so it might need tweaking, and ofcourse you need to change the variable names corresponding to your actual db rownames.

    Edit: In your code, you have to use $row and not $row2.

    Secondly, instead of this:

    <option value='{$id}'>{$lastname},{$firstname}</option>
    

    use this:

    <option value="' . $row['id'] . '">' . $row['lastname'] . ', ' . $row['firstname'] . '</option>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划