我想从数据库填充下拉菜单。我已经尝试了一些方法,但没有一种方法可以正常工作。我的代码如下所示:
<?php
$pdo = new PDO('mysql:host=1***7;dbname=databasename', 'username', 'password');
$sql = "SELECT user FROM users where tip='tip1'";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$users = $stmt->fetchAll();
?>
<select>
<?php foreach($users as $user): ?>
<option value="<?= $user['user']; ?>"></option>
<?php endforeach; ?>
</select>
答案 0 :(得分:0)
您忘记在选项<option value="<?= $user['user']; ?>"><?= $user['user']; ?></option>
<select>
<?php foreach($users as $user): ?>
<option value="<?= $user['user']; ?>"><?= $user['user']; ?></option>
<?php endforeach; ?>
</select>