我在 php 中创建了一个用户个人资料页面。用户插入性别和电话号码,所有这些都存储在数据库中的一个名为 profile 的表中。我的问题是如何制作下拉列表以保留用户上次输入的选定值。例如,如果用户选择男性,下拉列表应将男性保持为选中选项,并且仅在用户决定再次更改时才更改。
这是代码:
<form action="" method="POST" >
<?php
if ( isset($_GET['success']) === true && empty($_GET['success'])===true ){
echo'Profile Updated Sucessfuly';
}else{
if( empty($_POST) === false && empty($errors) === true ){
$update_data_profile = array('gender' => $_POST['gender'],
'telephone' => $_POST['telephone']);
update_user_profile($session_user_id, $update_data_profile);
header('Location: profile_update.php?success');
exit();
}else if ( empty($errors) === false ){
echo output_errors($errors);
}
?>
Gender<select name="gender" id="gender">
<option value=" "> EMPTY </option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
Telephone <input name="telephone" type="text" size="25" />
<input type="submit" value="" name="submit"/></br>
这是我用来在数据库中插入数据的函数:
function update_user_profile($user_id, $update_data_profile){
$result = mysql_query("select user_id from profile where user_id = $user_id limit 1");
$user_id = $update_data_profile['user_id'] ;
if(count($update_data_profile)){
$columns = array();
$values = array();
foreach($update_data_profile as $field => $data){
$columns[] = $field;
$values[] = $data;
}
}
mysql_query(" INSERT INTO `profile` (" . implode(",", $columns) .") values ('" . implode("','", $values) . "')" ) or die (mysql_error());