所以這是我非常簡單和基本的帳戶系統(只是一個學校項目),我希望用戶能夠更改他們的密碼。但我不確定如何在一行中更換密碼值,並保持所有其他值相同。如何使用php在CSV文件中的一行內替換1個值?
CSV文件:
ID,Username,Email,DateJoined,Password,UserScore,profilePics
1,Test,[email protected],03/12/2014,Test,10,uploads/profilePics/Test.jpg
2,Alfie,[email protected],05/12/2014,1234,136,uploads/profilePics/Alfie.png
PHP: ( 「cNewPassword」=確認新密碼)
<?php
session_start();
if(empty($_POST['oldPassword']) || empty($_POST['newPassword']) || empty($_POST['cNewPassword'])) {
die("ERROR|Please fill out all of the fields.");
} else {
if($_POST['newPassword'] == $_POST['cNewPassword']) {
if ($_POST['oldPassword'] == $_SESSION['password']) {
$file = "Database/Users.csv";
$fh = fopen($file, "w");
while(! feof($file)) {
$rows = fgetcsv($file);
if ($rows[4] == $_POST['oldPassword'] && $rows[1] == $_SESSION['username']) {
//Replace line here
echo("SUCCESS|Password changed!");
}
}
fclose($file);
}
die("ERROR|Your current password is not correct!");
}
die("ERROR|New passwords do not match!");
}
?>
好的,謝謝,你能解釋如何去做這個請嗎? –
創建3個循環,每個步驟一個,1.將數據加載到數組中,然後2.找到要更新的字段。最後,3.循環槽數組寫入更新的CSV文件,因爲它是一個學校項目,我不知道給你多少:P – Richard87
哈哈好吧謝謝:) –