我正在學習PHP,我很努力地找到解決我的問題的方法。我創建了一個可以編輯會員資格數據的頁面。我所有的'type = text'字段都正確顯示了成員的當前值。但是在2個下拉字段中選擇的值(語言和興趣)不會顯示在編輯字段中。他們雖然更新到MySql,但當我想編輯成員的「語言」和「興趣」字段時,顯示「選擇一個...」選項。用PHP中的下拉菜單更新會員表格
我該怎麼做才能使當前值存儲在數據庫中的2個下拉列表顯示在成員需要編輯的UI上?
這是我的PHP代碼:
<?php
$id = isset($_GET['id']) ? $_GET['id'] : die('ERROR: Record ID not Found');
include('dbconnect.php');
try{
$sql = "SELECT id, firstName, lastName, idNumber, mobileNumber, email, birthDate, languageType, interest FROM members WHERE id = ? LIMIT 0,1";
$stmt = $conn->prepare($sql);
$stmt->bindParam(1, $id);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$firstName = $row['firstName'];
$lastName = $row['lastName'];
$idNumber = $row['idNumber'];
$mobileNumber = $row['mobileNumber'];
$email = $row['email'];
$birthDate = $row['birthDate'];
$languageType = $row['languageType'];
$interest = $row['interest'];
}
catch(PDOException $exception){
die('ERROR: '.$exception->getMessage());
}
?>
<?php
$id = isset($_GET['id']) ? $_GET['id'] : die('ERROR: Record ID not found.');
include 'dbconnect.php';
if($_POST){
try{
$sql = "UPDATE members SET
firstName=:firstName,
lastName=:lastName,
idNumber=:idNumber,
mobileNumber=:mobileNumber,
email=:email,
birthDate=:birthDate,
languageType=:languageType,
interest=:interest
WHERE id=:id";
$stmt = $conn->prepare($sql);
$firstName = htmlspecialchars(strip_tags($_POST['firstName']));
$lastName = htmlspecialchars(strip_tags($_POST['lastName']));
$idNumber = htmlspecialchars(strip_tags($_POST['idNumber']));
$mobileNumber = htmlspecialchars(strip_tags($_POST['mobileNumber']));
$email = htmlspecialchars(strip_tags($_POST['email']));
$birthDate = htmlspecialchars(strip_tags($_POST['birthDate']));
$languageType = $_POST['languageType'];
$interest = $_POST['interest'];
$stmt->bindParam(':firstName', $firstName);
$stmt->bindParam(':lastName', $lastName);
$stmt->bindParam(':idNumber', $idNumber);
$stmt->bindParam(':mobileNumber', $mobileNumber);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':birthDate', $birthDate);
$stmt->bindParam(':languageType', $languageType);
$stmt->bindParam(':interest', $interest);
$stmt->bindParam(':id', $id);
if($stmt->execute()){
echo "<div class='alert alert-success'>Member was updated.</div>";
}else{
echo "<div class='alert alert-danger'>Unable to update member. Please try again.</div>";
}
}
catch(PDOException $exception){
die('ERROR: ' . $exception->getMessage());
}
}
?>
這裏是HTML:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"] . "?id={$id}");?>" method="post">
<table class='table table-hover table-responsive table-bordered'>
<tr>
<td>First Name</td>
<td><input type='text' name='firstName' value="<?php echo htmlspecialchars($firstName, ENT_QUOTES); ?>" class='form-control' /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type='text' name='lastName' value="<?php echo htmlspecialchars($lastName, ENT_QUOTES); ?>" class='form-control' /></td>
</tr>
<tr>
<td>ID Number</td>
<td><input type='text' name='idNumber' value="<?php echo htmlspecialchars($idNumber, ENT_QUOTES); ?>" class='form-control' /></td>
</tr>
<tr>
<td>Mobile Number</td>
<td><input type='text' name='mobileNumber' value="<?php echo htmlspecialchars($mobileNumber, ENT_QUOTES); ?>" class='form-control' /></td>
</tr>
<tr>
<td>Email</td>
<td><input type='text' name='email' value="<?php echo htmlspecialchars($email, ENT_QUOTES); ?>" class='form-control' /></td>
</tr>
<tr>
<td>Birth Date</td>
<td><input type='date' name='birthDate' value="<?php echo htmlspecialchars($birthDate, ENT_QUOTES); ?>" class='form-control' /></td>
</tr>
<tr>
<td>Language</td>
<td>
<select name='languageType' class='form-control' value="<?php echo $languageType; ?>" />
<option>Select One...</option>
<option>Afrikaans</option>
<option>English</option>
<option>Zulu</option>
<option>Xhosa</option>
<option>Venda</option>
<option>French</option>
</td>
</tr>
<tr>
<td>Interest</td>
<td>
<select name='interest' class='form-control' value="<?php echo htmlspecialchars($interest, ENT_QUOTES); ?>" />
<option>Select One...</option>
<option>Golf</option>
<option>Rugby</option>
<option>Tennis</option>
<option>Cricket</option>
<option>Swimming</option>
<option>Hiking</option>
<option>Surfing</option>
<option>Movies</option>
<option>Swords</option>
</td>
</tr>
<tr>
<td></td>
<td>
<input type='submit' value='Save Changes' class='btn btn-primary' />
<a href='index.php' class='btn btn-danger'>Back to read members</a>
</td>
</tr>
</table>
</form>
值必須在選擇不選擇 –