[英]Prevent updating column if null or empty variable in PDO
I am trying to have an update field that admin can update user information.我正在尝试有一个管理员可以更新用户信息的更新字段。 I am stuck at the password.
我被密码困住了。 If I update now and leave the password blank then it hashes the blank value and inserts it into DB.
如果我现在更新并将密码留空,那么它会散列空白值并将其插入到数据库中。 I want to keep previous value unless changed on the form.
我想保留以前的值,除非在表单上更改。
I looked at this as it was closest to what I am looking for PHP MYSQL -> UPDATE column with variable if that variable isn't null or empty This did not answer my question.我看着这个,因为它最接近我正在寻找的PHP MYSQL -> 带有变量的 UPDATE 列,如果该变量不为 null 或为空这没有回答我的问题。 unless I have multiple queries I guess with if statements.
除非我有多个查询,否则我猜是 if 语句。 Really I only want it to update edited fields.
我真的只希望它更新编辑过的字段。
if(isset($_POST['register'])){
error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
$username = !empty($_REQUEST['username']) ? trim($_REQUEST['username']) : null;
$pass = !empty($_REQUEST['password']) ? trim($_REQUEST['password']) : null;
$fname = !empty($_REQUEST['fName']) ? trim($_REQUEST['fName']) : null;
$lname = !empty($_REQUEST['lName']) ? trim($_REQUEST['lName']) : null;
$role = !empty($_REQUEST['role']) ? trim($_REQUEST['role']) : null;
$region = !empty($_REQUEST['region']) ? trim($_REQUEST['region']) : null;
$district = !empty($_REQUEST['district']) ? trim($_REQUEST['district']) : null;
$id = !empty($_REQUEST['ID']) ? trim($_REQUEST['ID']) : null;
$location = !empty($_REQUEST['store']) ? trim($_REQUEST['store']) : null;
$outlet = !empty($_REQUEST['outletid']) ? trim($_REQUEST['outletid']) : null;
$passwordHash = password_hash($pass, PASSWORD_BCRYPT, array("cost" => 12));
$sql=$pdo->prepare("UPDATE users
SET username = :username,
password = :password,
fName = :fName,
lName = :lName,
role = :role,
region = :region,
district = :district,
outlet_id = :outlet_id,
store_name = :store_name
WHERE id = :id
");
$sql->bindParam(':username',$username);
$sql->bindParam(':password',$passwordHash);
$sql->bindParam(':fName',$fname);
$sql->bindParam(':lName',$lname);
$sql->bindParam(':role',$role);
$sql->bindParam(':region',$region);
$sql->bindParam(':district',$district);
$sql->bindParam(':id',$id);
$sql->bindParam(':outlet_id',$outlet);
$sql->bindParam(':store_name',$location);
if($sql->execute()){
$_SESSION['success'] = "Successfully Updated Record";
}// End of if profile is ok
else{
print_r($sql->errorInfo()); // if any error is there it will be posted
$msg=" Database problem, please contact site admin ";
}
}
I tried using CASE within the statement but that throws an error.我尝试在语句中使用 CASE 但这会引发错误。 How can I update user data without removing data in the column?
如何在不删除列中的数据的情况下更新用户数据?
IE: If username is changed then just edit that field in the DB. IE:如果用户名更改,则只需在数据库中编辑该字段。 If username and password are changed update both.
如果用户名和密码被更改,则更新两者。
Thank you guys谢谢你们
EDIT:编辑:
I tried this我试过这个
if(!empty(trim($_REQUEST['password']))){
$passwordHash = password_hash($pass, PASSWORD_BCRYPT, array("cost" => 12));
}
This still updates the password这仍然更新密码
Edit2:编辑2:
"UPDATE users
SET username=:username,
password = CASE
WHEN :password = '' THEN password
ELSE :password
END,
fName=:fName,
lName=:lName,
role=:role,
region=:region,
district=:district,
outlet_id=:outlet_id,
store_name=:store_name
WHERE id=:id");
It is right approach.这是正确的做法。 But you should also use else method, to keep password same, if password field is empty.
但是如果密码字段为空,您也应该使用 else 方法来保持密码相同。
If you want to update your profile:如果您想更新您的个人资料:
$userSql = $pdo->prepare("SELECT * FROM users where id = :id");
$userSql->bindParam(':id',$id);
$userSql->execute();
$user = $userSql->fetch();
if(!empty(trim($_REQUEST['password']))){
$passwordHash = password_hash($pass, PASSWORD_BCRYPT, array("cost" => 12));
}
else {
$passwordHash = $user['password']; //user password from db
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.