带有数据库信息的 MySQL 和 PHP 选择选项 [英] MySQL and PHP Select Option with information from database
问题描述
这是我的项目到目前为止我有这个我从我的项目的一部分中获取了代码
<?php error_reporting(E_ERROR | E_PARSE);?><!DOCTYPE html><头><title>来电</title><script type="text/javascript" src="Javascript/jquery.js"></script><script type="text/javascript" src="Javascript/jquery-1.11.1.min.js"></script><script type="text/javascript" src="Javascript/employee_number_autocomplete.js"></script><link href="css/create.css" rel="stylesheet" type="text/css" >头部><body style="background-image: url('http://a0319p528/new_callins/images/background.jpg')"><div align="center">
<h1 class="h1">Call Ins Entry Page<br><br></h1><div align="center"><form action="insert_process.php" method="post" class="form" style="width: 700px; height: 575px"><br><br><br><br><br><表格样式=宽度:81%"><tr><!--预定日期的开始--!><td style="width: 133px">预定日期</td><td style="width: 152px"><input name="Date_Scheduled" id="Date_Scheduled" type="text"value="<?php echo date("M j, Y - g:i"); ?>"/></td><!--预定日期结束--!><!--推理的开始--!><td style="width: 159px">原因</td><td><select name="Reason" style="width: 160px" ><option value="">请选择...</option></select></td></tr><!--推理结束--!><!--员工姓名开头--!><tr><td style="width: 133px">员工姓名</td><td style="width: 152px"><input type="text" id="Employee_Name" name="Employee_Name"></td><!--员工姓名结尾--!><!--联系开始--!><td style="width: 159px">联系方式</td><td><选择名称=联系人"样式=宽度:160像素"><option value="">请选择...</option></select></td></tr><!--联系结束--!><!--员工编号开头--!><tr><td style="width: 133px">员工编号</td><td style="width: 152px"><input type="text" id="Employee_Number" name="Employee_Number" ></td><!--员工编号结尾--!><!--开始审批--!><td style="width: 159px">批准</td><td><选择名称=批准"样式=宽度:160px"><option value="">请选择...</option></select></td></tr><!--批准结束--!><tr><td style="width: 133px">Time_Reported</td><td style="width: 152px"><input name="Time_Reported" id="Time_Reported" type="text"value="<?php echo date("M j, Y - g:i");?>"/></td></td><!--批准结束--!><!--预定区域开始--!><td style="width: 159px">预定区域</td><td><select name ="Scheduled_Area" style="width: 160px" ><option value="">请选择...</option></select></td></tr><!--预定区域结束--!><br><input type="submit" value="提交请求"><br><br><br><br><!--开始是或否复选框--!><p class="style1">不要勾选以下方框,仅供人力资源使用<strong>.</strong></p><label>no</label><input type="checkbox" id="Complete" name="Complete" value="N"><标签>是</标签><input type="checkbox" id="Complete" name="Complete" value="Y"><br><!--否或是复选框的结尾--!></表单>
</html>
有没有办法用mysql db和php做一个select选项
但是让选项值从数据库名字和姓氏的两列中获取名称.如果有人遇到任何示例或教程,请分享,我将继续谷歌它.
谢谢
我会给你一个简短的例子.
现在,您的代码将为您提供 1 个选项
让我们以一个数组为例:
$array = array('0' => 'test', '1' => 'test1');
要将数组填充为选项,您只需执行
它将为您提供数组中的所有值作为选项.
<小时>您现在可以使用数据库中的数据填充数组
$con = new mysqli('HOST', 'DB_USER', 'DB_PASS', 'DB');$sql = "SELECT * FROM TABLE");if ($result = $con->query($sql)) {while ($row = $result->fetch_assoc()) {$array = array('0' => $row['row1'], '1' => $row['row2']);}}
现在您已经用 db 中的数据填充了您的数组,并且可以根据需要将其填充到您的选项中.
This is my project so far I have this I took the code from part of my project
<?php error_reporting(E_ERROR | E_PARSE); ?>
<!DOCTYPE html>
<html>
<head>
<title>Call Ins </title>
<script type="text/javascript" src="Javascript/jquery.js"></script>
<script type="text/javascript" src="Javascript/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="Javascript/employee_number_autocomplete.js"></script>
<link href="css/create.css" rel="stylesheet" type="text/css" >
</head>
<body style="background-image: url('http://a0319p528/new_callins/images/background.jpg')">
<div align="center">
</div>
<h1 class="h1">Call Ins Entry Page<br><br></h1>
<div align="center">
<form action = "insert_process.php" method ="post" class="form" style="width: 700px; height: 575px">
<br><br><br><br><br>
<table style="width: 81%">
<tr>
<!--start of Scheduled Date--!>
<td style="width: 133px">Scheduled Date</td>
<td style="width: 152px">
<input name="Date_Scheduled" id="Date_Scheduled" type="text"value="<?php echo date("M j, Y - g:i"); ?>"/>
</td>
<!--end of Scheduled Date--!>
<!--start of reason--!>
<td style="width: 159px">Reason</td>
<td>
<select name ="Reason" style="width: 160px" >
<option value ="">Please select ...</option></select>
</td></tr>
<!--end of reason--!>
<!--start of Employee Name--!>
<tr>
<td style="width: 133px">Employee Name</td>
<td style="width: 152px">
<input type="text" id="Employee_Name" name="Employee_Name">
</td>
<!--end of Employee Name--!>
<!--start of Contact--!>
<td style="width: 159px">Contact</td><td>
<select name ="Contact" style="width: 160px">
<option value ="">Please select ...</option></select>
</td></tr>
<!--end of Contact--!>
<!--start of Employee Number--!>
<tr>
<td style="width: 133px">Employee Number</td>
<td style="width: 152px">
<input type="text" id="Employee_Number" name="Employee_Number" ></td>
<!--end of Employee Number--!>
<!--start of Approval--!>
<td style="width: 159px">Approval</td>
<td>
<select name ="Approval" style="width: 160px">
<option value ="">Please select ...</option></select>
</td>
</tr>
<!--end of Approval--!>
<tr>
<td style="width: 133px">Time_Reported</td>
<td style="width: 152px"><input name="Time_Reported" id="Time_Reported" type="text"value="<?php echo date("M j, Y - g:i"); ?>"/></td>
</td>
<!--end of Approval--!>
<!--start of Scheduled Area--!>
<td style="width: 159px">Scheduled Area</td>
<td><select name ="Scheduled_Area" style="width: 160px" >
<option value ="">Please select ...</option></select></td>
</tr>
<!--end of Scheduled Area --!>
</table>
<br>
<input type="submit" value= "Submit Request "><br><br><br><br>
<!--start of yes or no Checkbox--!>
<p class="style1">Do not check Box bellow human resources use only<strong>.</strong></p>
<label>no</label>
<input type="checkbox" id="Complete" name="Complete" value="N">
<label>yes</label>
<input type="checkbox" id="Complete" name="Complete" value="Y"> <br>
<!--end of no or yes checkbox--!>
</form>
</div>
</body>
</html>
<?php
header('Content-Type: application/json');
$db_conx = mysqli_connect("localhost", "root", "systems399", "employees_db");
$Employee_Name= $_POST["Employee_Name"];
$sql="SELECT * FROM names WHERE FIRSTNAME='$Employee_Name' ";
$query= mysqli_query($db_conx, $sql);
$row = mysqli_fetch_array($query, MYSQLI_ASSOC);
$rc= $row["EMPLOYEE_NUMBER"];
echo json_encode ($rc);
?>
Is there a way to make a select option with mysql db and php
<select name ="Employee Name" style="width: 160px" >
<option value ="">Please select ...</option></select>
But have the option value get the names from the two columns in the database Firstname and lastname. I will continue to Google it if anyone came across any examples or tutorials please share.
Thank you
I'll give you a short example.
Right now, you'r code will give you 1 option
<select name ="Employee Name" style="width: 160px" >
<option value ="">Please select ...</option></select>
Let's take an array like:
$array = array('0' => 'test', '1' => 'test1');
To populate your array as options, you can simply do
<select>
<?php
foreach ($array as $a) {
?>
<option value=""><?= $a ?></option>
<?php
}
?>
</select>
which will give you all values from your array as an option.
You can now go and fill your array with data from your db by
$con = new mysqli('HOST', 'DB_USER' , 'DB_PASS' , 'DB');
$sql = "SELECT * FROM TABLE");
if ($result = $con->query($sql)) {
while ($row = $result->fetch_assoc()) {
$array = array('0' => $row['row1'], '1' => $row['row2']);
}}
Now you have filled your array with the data from db and can populate this in your options as you want.
这篇关于带有数据库信息的 MySQL 和 PHP 选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!