mysql数据库使用已经在html / php中输入的数据制作下拉菜单 [英] mysql database making drop down menu using data already entered in html/php

查看:125
本文介绍了mysql数据库使用已经在html / php中输入的数据制作下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含3个表的数据库:spusername,splocation,sprecord。 spusername具有id,splocation_id,lastname,firstname。我希望能够从数据库中获得一个下拉菜单,其中包含id,lastname和firstname,在下拉菜单中它只显示所有名称都是lastname,firstname的列表。那么一旦我选择了一个人,我有另一个下拉菜单,里面有训练类型。那么当我点击提交时,它将在另一个表中生成一个记录,其中包含人员ID和培训记录。所以当我做搜索它会拉起用户和该人的培训记录....我已经创建了一个.php的提交页面,为新用户发送lastname,firstname,splocation_id,我想我可以创建一个搜索,做我想做的,但我从来没有做一个数据录入做一个下拉,从数据库中产生的值。



编辑代码:在Vegard的编码我得到了这个,现在它经过几次试验和错误后效果很好。
$ b

代码:

 <?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>感谢您< / h1>您的资讯已输入资料库< br>< br> ;
} else {
echoERROR:.mysql_error();
}
} else {
?>
< h1>将训练信息添加到数据库< / h1>< hr>
< br>< br>
< form method =postaction =>
< select name =spusername_id>
< option value =default>选择Employee< / option>
<?php
include(connectionspusers.php); / *文件,你已经存储了数据库连接。设置* /
$ 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']。 < /选项>;
}
?>
< / select>

< select name =sptraining_id>
< option value =default>选择培训< / option>
<?php
include(connectingptraining.php); / *文件,你已经存储了数据库连接。设置* /
$ 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']。 < /选项>;
}
?>
< / select>
< br>< br>
< input type =submitname =Submitvalue =Submit>
< / form>
<?php
}
?>


解决方案

b

 < select name =pulldown1> 
< option value =default>选择一个选项< / option>
<?php
include(connect.php); / *文件,你已经存储了数据库连接。设置* /
$ 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)。 < /选项>;
}
?>
< / select>

< select name =pulldown2>
< option value =default>选择和选项< / 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']。 < /选项>;
}
?>
< / select>

这将导致两个下拉菜单,其中第一个下拉列表中列出了用户的最后名字和名字,逗号+空格,第二个将列出不同类型的培训。 ID字段只能通过变量发送,但不会显示给用户。



从pulldown1中的变量中提取值时,只需使用explode:

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

尚未测试代码,因此可能需要调整,因此您需要更改变量名称对应于你的实际数据库名称。



编辑:在你的代码中,你必须使用$ row而不是$ row2。

其次,不是这样:

 < option value = '{$ ID}' > {$姓氏},{$姓名}< /选项> 

使用此项:

 < option value ='。$ row ['id']。'>'。 $ row ['lastname']。 ','。 $ row ['firstname']。 < /选项> 


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 
} 
?> 

解决方案

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>

这篇关于mysql数据库使用已经在html / php中输入的数据制作下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆