Javascript 将数据从数据库加载到文本框
我有一个名为“employees”的mysql数据库表 它有3列,分别命名为“id”、“名称”和“薪水” 我有一个html/php网页,其中包含名为id和name的文本框。 我想将拥有最高工资的员工的“id”和“姓名”从数据库加载到这些文本框中。Javascript 将数据从数据库加载到文本框,javascript,php,Javascript,Php,我有一个名为“employees”的mysql数据库表 它有3列,分别命名为“id”、“名称”和“薪水” 我有一个html/php网页,其中包含名为id和name的文本框。 我想将拥有最高工资的员工的“id”和“姓名”从数据库加载到这些文本框中。 <?php //Defining constants for database connection best to store in seperate file and include that file const DB_HOST
<?php
//Defining constants for database connection best to store in seperate file and include that file
const DB_HOST = 'SERVER';
const DB_USER = 'USER';
const DB_PASS = 'PASSWORD';
const DB_NAME = 'php_mysql_login_system';
//Connecting to the database -- best use pdo
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
# check connection
if ($mysqli->connect_errno) {
echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
exit();
}
$sql = " SELECT * from `employees` "; // query data from databse -- alhough I suggest you use pdo
$result = $mysqli->query($sql);
//Looping through each results
foreach ($result as $value) {
// Store the employess salary and name in an array say employee['name'=>salaray] -- name as key and salary as value
}
$maximumSalary = max(//array of the employee i.e the one you have made above employee[]);
//after getting the maximum value use array_keys(array,value) to get the employee name
$employeeName = array_keys($employee,$maximumSalary);
}
?>
一个让您开始的示例代码希望这对您有所帮助,尽管此代码可能没有遵循最佳实践,但您至少需要开始一些东西,因为我认为您在开始时遇到了问题。您可以发布您迄今为止的代码和您尝试过的任何内容吗,谢谢