我正在编辑管理详细信息并且它正在工作,但我遇到了一个有关密码的问题。
我的问题是,如果密码字段为空,则仍然更新现有密码,并且我在数据库中获取空数据。
我想要的是,如果密码字段中有数据,则更新它,否则在更新时忽略密码字段。
我正在使用下面的代码
function updateAdmin($pdo){
$a_firstname=sanitize_data($_POST['editfirstname']);
$a_lastname=sanitize_data($_POST['editlastname']);
$a_email=$_POST['editemail'];
$a_accessrole=sanitize_data($_POST['editaccessrole']);
$admin_id=sanitize_data($_POST['admin_id']);
if(!empty($_POST['editpassword'])){
$a_password=sanitize_data($_POST['editpassword']);
$password=password_hash($a_password, PASSWORD_BCRYPT,['cost' => 12]);
}
$data=array(
'a_firstname' => $a_firstname,
'a_lastname' => $a_lastname,
'a_email' => $a_email,
'a_password' => $password,
'a_accessrole' => $a_accessrole
);
try{
$sql = "UPDATE tbl_admin SET a_firstname=:a_firstname, a_lastname=:a_lastname, a_email=:a_email, a_password=:a_password, a_accessrole=:a_accessrole WHERE admin_id=:admin_id";
$stmt= $pdo->prepare($sql);
$stmt->bindParam('admin_id', $admin_id);
$stmt->execute($data);
$response['error'] = "true";
} catch(PDOExecption $e) {
$dbh->rollback();
print "Error!: " . $e->getMessage() . "</br>";
$response['error'] = "false";
}
// print_r($response);
echo json_encode($response);
}
function sanitize_data($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = strip_tags($data);
$data = htmlspecialchars($data);
return $data;
}
holdtom
繁华开满天机