MySQL和PHP从数据库中选择信息选项


MySQL and PHP Select Option with information from database

这是我的项目到目前为止,我已经有了这个,我从我的项目中提取了代码

<?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);
 ?>

有没有一种方法可以使用mysqldb和php进行选择

<select name ="Employee Name" style="width: 160px" >
<option value ="">Please select ...</option></select>

但是让选项值从数据库Firstname和lastname的两列中获取名称。如果有人遇到任何例子或教程请分享,我将继续用谷歌搜索。

感谢

如果我理解得对,您只想从数据库中选择数据并在HTML中使用它。这是一个非常基本的问题。如果你不知道如何做到这一点,我会先推荐一本"如何学习MySQL"的书。开发一个必须在线询问每一行代码的网站不会很有趣。

编辑:好的,现在有一些代码。这应该有效:

<option value="<?php echo $rc; ?>"><?php echo $row["FIRSTNAME"]</option>

但是,请不要在公共网站中使用您的代码。这不是问题的主题,但您的代码中存在一个巨大的SQL注入漏洞。

我将给您举一个简短的例子。

现在,你的代码会给你1个选项

<select name ="Employee Name" style="width: 160px" >
<option value ="">Please select ...</option></select>

让我们取一个数组,如:

$array = array('0' => 'test', '1' => 'test1');

要将数组填充为选项,您可以简单地执行

<select>
<?php
foreach ($array as $a) {
?> 
<option value=""><?= $a ?></option>
<?php
}
?>
</select>

这将为您提供数组中的所有值作为选项。


您现在可以通过将数据库中的数据填充到阵列中

$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']);
}}

现在,您已经用数据库中的数据填充了数组,并且可以根据需要在选项中填充这些数据。

您可以从客户端创建一个对话框,用于输入名字和姓氏,然后将这些值传递给php类,并使用这些变量从数据库中进行选择。