drtj40036 2013-03-03 16:38
浏览 32
已采纳

表单保存空值php

I have created a profile page in php, where the user, using a form can save his telephone number in a table named profile in database. If the user decides to change his telephone number can easily go to form change it and save it again. My only problem is the following. Suppose that the user wants to delete his telephone number. If he goes to the form delete his number (makes the field empty) and press save , the telephone number doesn't change to an empty value and continues to keep the previous number. Any idea how to change this in order to keep an empty value?

This is my code with the form :

<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('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);
       }

     ?>

 Telephone<input name="telephone" type="text" size="25"  value="<?php echo $user_data_profile['telephone']; ?>"/>
<input type="submit" value="" name="submit"/>
</form> 

And this is my function that dispatches data to profile table:

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

}

}
  • 写回答

3条回答 默认 最新

  • dongpo8702 2013-03-03 16:39
    关注

    You are only updating the field if the data is NOT empty.

    See this line:

    if(!empty($data)){
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错