0

我在 php 中创建了一个个人资料页面,用户使用 html 下拉列表选择性别。在用户选择性别后,表单会在函数的帮助下发送数据并将数据保存到配置文件表中。我想要的只是下拉列表以保留用户上次选择的值。例如,假设用户制作了他的个人资料并选择了性别=男性。如果他想在下次访问个人资料页面时更新他的个人资料,则下拉列表将“男性”作为选定值。

这是我的代码:

<?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'],
            'zip' => $_POST['zip']);


        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&nbsp;<select name="gender"  id="gender"> 
    <option value=" "> EMPTY </option> 
    <option value="Male">Male</option> 
    <option value="Female">Female</option> 
</select>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; 


ZIP<input name="zip" type="text" size="15" placeholder="type your ZIP code"/>

这是我的功能:

<?php
function update_user_profile($user_id, $update_data_profile){

    $result = mysql_query("select user_id from profile where user_id = $user_id limit 1");

    if(mysql_num_rows($result) === 1) {

        $update = array();

        array_walk($update_data_profile, 'array_sanitize');

        foreach($update_data_profile as $field => $data ){

            if(!empty($data)){
                $update[]='`' . $field . '` = \'' . $data . '\'';
            }
        }

        if(isset($update) && !empty($update)) {

            mysql_query(" UPDATE `profile` SET " . implode(',  ', $update) . " WHERE `user_id` = $user_id ") or die(mysql_error());
        }
    }
    else {

        $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());

    }

}
?>
4

1 回答 1

0

要从数据库中获取性别并默认显示在下拉列表中,您需要执行类似的操作。

  $con=mysqli_connect("example.com","peter","abc123","my_db");
  // Check connection
  if (mysqli_connect_errno())
  {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  $result = mysqli_query($con,"select * from profile where user_id = $user_id limit 1");

  while($row = mysqli_fetch_array($result))
  {
   $gender = $row['gender']; 
  }

  mysqli_close($con);

  if($gender == "Male"){
echo'
Gender&nbsp;<select name="gender"  id="gender"> 
<option value="Male" selected>Male</option> 
<option value="Female">Female</option> 
</select>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; 
';
  } else {
 echo' Gender&nbsp;<select name="gender"  id="gender"> 
<option value="Male" >Male</option> 
<option value="Female" selected>Female</option> 
</select>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; ';
  }
于 2013-02-27T10:51:35.997 回答