PHPでユーザープロファイルページを作成しました。ユーザーは性別と電話番号を挿入し、これらはすべてデータベースのプロファイルと呼ばれるテーブルに保存されます。私の質問は、ユーザーが以前に入力した選択した値を保持するためにドロップダウンリストを作成するにはどうすればよいですか。たとえば、ユーザーが男性を選択した場合、ドロップダウンリストは男性を選択したオプションとして保持し、ユーザーが再度変更することを決定した場合にのみ変更する必要があります。
コードは次のとおりです。
<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());