dongtuo4132 2017-11-27 09:02
浏览 21

PHP PDO动态更新查询到MYSQL

I have a form with an image upload and text inputs. it keeps replacing the profile_picture field with NULL. Therefore, I'm trying to create a dynamic update query, where if one value is empty it's excluded from the query altogether.

Any help is appreciated.

IMAGE UPLOAD:

if (!empty($_FILES['profile_picture']) && $_FILES['profile_picture']['error'] == UPLOAD_ERR_OK) {

    // Rename the uploaded file
    $uploadName = $_FILES['profile_picture']['name'];
    $tmp_name = $_FILES['profile_picture']['tmp_file'];
    $ext = strtolower(substr($uploadName, strripos($uploadName, '.')+1));
    $filename = round(microtime(true)).mt_rand().'.'.$ext;

if (move_uploaded_file($_FILES['profile_picture']['tmp_name'],'../profile_picutres/'. $filename)) {

}

}

UPDATE QUERY:

$stmt = $dbh->prepare("UPDATE 001_user_table_as SET profile_picture=:profile_picture, first_name=:first_name, last_name=:last_name, phone_number=:phone_number, nationality=:nationality, years_experience=:years_experience, data=:data WHERE id=:id");

 $stmt->bindParam(':profile_picture', $filename);
 $stmt->bindParam(':first_name', $first_name);
 $stmt->bindParam(':last_name', $last_name);
 $stmt->bindParam(':phone_number', $phone_number);
 $stmt->bindParam(':nationality', $nationality);
 $stmt->bindParam(':years_experience', $years_experience);
 $stmt->bindParam(':data', $cv_data);
 $stmt->bindParam(':id', $user_id);

if($stmt->execute()){
$response["message"] = 'success';   
}else{
$response["message"] = 'error'; 
$errors++;
} 
  • 写回答

1条回答 默认 最新

  • duanbo7517 2017-11-27 12:48
    关注

    Below is the solution, where an input is empty, it'll use the existing data in that field and will accept not only $_POST variables, but all variables.

    // the list of allowed field names
    $allowed = ["profile_picture","first_name","last_name", "phone_number", "nationality", "years_experience", "data" ];
    
    // initialize an array with values:
    $params = [];
    
    // initialize a string with `fieldname` = :placeholder pairs
    $setStr = "";
    
    // loop over source data array
    foreach ($allowed as $key)
    {
        if (!empty([$key]) || $key != "" || $key != NULL)
        {
    
            if($GLOBALS[$key] != NULL){
    
            $setStr .= "`$key` = :$key ,";
            $params[$key] = $GLOBALS[$key];
    
            }else{
    
            $setStr .= "`$key` = $key ,";
    
            }
    
        }else{
    
    
    
        }
    }
    $setStr = rtrim($setStr, ",");
    
    $params['id'] = $_SESSION['user_id'];
    
    $dbh->prepare("UPDATE 001_user_table_as SET $setStr WHERE id = :id")->execute($params);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题