【发布时间】:2013-02-28 17:00:40
【问题描述】:
我在 php.ini 中创建了一个用户个人资料页面。用户插入性别和电话号码,所有这些都存储在数据库中的一个名为 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());
【问题讨论】:
-
What have you tried?是的,您已经向我们提供了您所做工作的代码,但是您尝试过什么来完成您的问题?提示:HTML Option Selected
-
还没有我不知道怎么做
-
为什么不将这些发布的值存储在用户会话或 cookie 中,如果它们存在,则默认表单中的值
-
所以您希望下次用户点击该页面时从数据库中填充下拉列表?
-
是的,可能是要从数据库中填充的下拉列表...我无法想象任何其他方式
标签: php drop-down-menu