當用戶編輯他/她自己的帳戶時,如果他/她的現有帳戶使用電子郵件應該能夠將其更新到phonenumber
,反之亦然。如何在編輯帳戶時刪除JSON密鑰?
我以爲是檢查輸入的格式,如果輸入的是一個email
和老一個是phonenumber
然後刪除與phonenumber
鑰匙,創建一個新的與email
否則只是更新email
關鍵的價值。和phonenumber
一樣的故事。
下面的數組是我在users.txt文件中的用戶。
[
{
"role": "admin",
"id": "59df4ef2d8d39",
"email": "[email protected]",
"name": "A",
"lastname": "A",
"password": "1",
"image": "img_webshop\/userimage-59dfb91515810.png"
},
{
"role": "user",
"id": "59df4f1b070e6",
"phonenumber": "12345678",
"name": "B",
"lastname": "B",
"password": "2",
"image": "img_webshop\/userimage-59e37de69475b.png"
},
{
"role": "user",
"id": "59dfc0cb07985",
"email": "[email protected]",
"name": "C",
"lastname": "C",
"password": "3",
"image": "img_webshop\/userimage-59dfc0cb06c5f.png"
},
{
"role": "user",
"id": "59dfc22f26f78",
"phonenumber": "87654321",
"name": "D",
"lastname": "D",
"password": "4",
"image": "img_webshop\/userimage-59dfc22f2638d.png"
},
{
"role": "user",
"id": "59dfc460b261e",
"email": "[email protected]",
"name": "E",
"lastname": "E",
"password": "5",
"image": "img_webshop\/userimage-59dfc460af866.png"
},
{
"role": "user",
"id": "59e75231a393c",
"email": "[email protected]",
"name": "Y",
"lastname": "Y",
"password": "",
"image": "img_webshop\/userimage-59e79184d0335."
}
]
而這是處理更新的PHP API。
//GETTING FROM FILE:
$sajUsers = file_get_contents('users.txt');
$ajUsers = json_decode($sajUsers);
//_________________________________________________________//
// getting it from the front end:
$sUserId = $_POST['txtUpdateUserId'];
$sNewUserRole = $_POST['txtUpdateUserRole'];
$sNewUserName = $_POST['txtUpdateUserName'];
$sNewUserLastName = $_POST['txtUpdateUserLastName'];
$sNewUserEmailorPhoneNumber = $_POST['txtUpdateUserEmailorPhoneNumber'];
$sNewUserPassword = $_POST['txtUpdateUserPassword'];
//_________________________________________________________//
$match_found = false;
//The is getting it from the database.
for ($i = 0; $i < count($ajUsers); $i++) {
if ($sUserId == $ajUsers[$i]->id) { //checks if the value of the username is equal to the value in the array.
$ajUsers[$i]->role = $sNewUserRole;
if (fnCheckEmailFormat ($sNewUserEmail)) { // call the function which checks if is a valid email
if ($ajUsers[$i]->phonenumber) {
//HOW TO DELETE THE RESPECTIVE PHONENUMBER KEY
$ajUsers[$i]->email = $sNewUserEmailorPhoneNumber; //AND INSTEAD ASSIGN AN EMAIL KEY
}
else {
$ajUsers[$i]->email = $sNewUserEmailorPhoneNumber;
}
}
else if (fnCheckDigitFormat ($sNewUserPhoneNumber)) { // call the function which checks that it should only contain digits
if ($ajUsers[$i]->email) {
//HOW TO DELETE THE RESPECTIVE EMAIL KEY
$ajUsers[$i]->phonenumber = $sNewUserEmailorPhoneNumber;
}
else {
$ajUsers[$i]->phonenumber = $sNewUserEmailorPhoneNumber;
}
}
$ajUsers[$i]->name = $sNewUserName;
$ajUsers[$i]->lastname = $sNewUserLastName;
$ajUsers[$i]->password = $sNewUserPassword;
$ajUsers[$i]->image = $sNewUserImageUrl;
$match_found = true;
break;
}
}
//_________________________________________________________//
if($match_found) {
//PUTTING TO FILE:
$sajNewUsers = json_encode($ajUsers, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE );
file_put_contents('users.txt', $sajNewUsers);
echo $sjResponse = '{"update":"ok"}';
exit; //end the if statement and exit if it works.
}
else {
echo $sjResponse = '{"update":"error"}'; // it didnt work.
exit;
}
//_________________________________________________________//
function fnCheckEmailFormat ($sNewUserEmail){ //checks if the property is valid. Called in line 6.
$sNewUserEmail = $sNewUserEmailorPhoneNumber;
if (!filter_var($sNewUserEmail, FILTER_VALIDATE_EMAIL)){
return false; // returns false if its not valid. Then it wont run the if.
}
return true; // else it will run the signin.
}
function fnCheckDigitFormat ($sNewUserPhoneNumber){ //checks if the property is valid. Called in line 6.
$sNewUserPhoneNumber = $sNewUserEmailorPhoneNumber;
if (!preg_match("/^[0-99]+$/", $sNewUserPhoneNumber)){
return false; // returns false if its not valid. Then it wont run the if.
}
return true; // else it will run the signin.
}
?>
您是否將所有的userdetails保存在一個文本文件中?用純文本密碼? – Andreas
它只是爲了假人而不是真正的項目。 – codeDragon