好的,首先我认为,您需要从数据库中获取一些数据并将其作为选项显示在选择框中,然后您需要将该选择选项值保存到数据库中。
- 首先你必须从数据库中获取值
function showAllData(){
$connection = mysqli_connect('localhost', 'root', '', 'beva');
$query = "SELECT * FROM users";
$result = mysqli_query($connection, $query);
if(!$result){
die('Query Failed'. mysqli_error());
}
while($row = mysqli_fetch_assoc($result)){
$id = $row['id'];
echo $id;
echo "<option name='$id'>$id</option>";
}
}
- 那么你必须在选择选项上调用这个函数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>
</head>
<body>
<div class="container mt-3 mb-5">
<div class="card">
<form action="html.php" method="post">
<input type="text" name="username" placeholder="Enter Username">
<input type="password" name="password" placeholder="Enter Password">
<select name="se"> <?php showAllData();?> </select>
<input type="submit" name="submit">
</form>
</div>
</div>
</body>
</html>
在这里,您只需调用该函数。确保您已<select name="se">
为选择框命名
- 那么您要做的就是将该值保存在数据库中
if(isset($_POST['submit'])){
$connection = mysqli_connect('localhost', 'root', '', 'beva');
$username = $_POST['username'];
$password = $_POST['password'];
$id = $_POST['se'];
echo $id;
$query = "UPDATE users SET username = '$username', password = '$password' WHERE id = $id ";
$result = mysqli_query($connection, $query);
echo $query;
if(!$result){
echo $query;
die("Query Failed" . mysqli_error($connection));
}
}
- 然后它会更新你的记录,在这里你不必一一创建列表