2015-01-04 47 views
1

所以這是我非常簡單和基本的帳戶系統(只是一個學校項目),我希望用戶能夠更改他們的密碼。但我不確定如何在一行中更換密碼值,並保持所有其他值相同。如何使用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!"); 
} 
?> 

回答

4

您必須以讀取模式打開文件,在寫入模式下打開臨時文件,寫入修改後的數據,然後刪除/重命名文件。我建議嘗試使用它來建立一個真正的數據庫和工作,但如果你要爲CSV,代碼看起來應該或多或少是這樣的:

$input = fopen('Database/Users.csv', 'r'); //open for reading 
$output = fopen('Database/temporary.csv', 'w'); //open for writing 
while(false !== ($data = fgetcsv($input))){ //read each line as an array 

    //modify data here 
    if ($data[4] == $_POST['oldPassword'] && $data[1] == $_SESSION['username']) { 
     //Replace line here 
     $data[4] = $_POST['newPassword']; 
     echo("SUCCESS|Password changed!"); 
    } 

    //write modified data to new file 
    fputcsv($output, $data); 
} 

//close both files 
fclose($input); 
fclose($output); 

//clean up 
unlink('Database/Users.csv');// Delete obsolete BD 
rename('Database/temporary.csv', 'Database/Users.csv'); //Rename temporary to new 

希望它能幫助。

0

你需要3個步驟來做到這一點(創建3個迴路,可以優化到1或2個環):

  1. 將相關數據加載到存儲器
  2. 更新所需的數據
  3. 將數據保存到文件

祝你好運! :)

PS。此外,您的密碼不應以明文形式存儲在內存(會話)或磁盤(csv)中,請使用散列功能!

+0

好的,謝謝,你能解釋如何去做這個請嗎? –

+1

創建3個循環,每個步驟一個,1.將數據加載到數組中,然後2.找到要更新的字段。最後,3.循環槽數組寫入更新的CSV文件,因爲它是一個學校項目,我不知道給你多少:P – Richard87

+0

哈哈好吧謝謝:) –

1

我的建議是我這會變成你的數據庫中的數據到一個數組,你可以修改,然後返回到原始狀態的小功能:

在該組功能,你只需要精確怎麼每一行/行數據是分開的。

function dbToArray($db, $row_separator = "\n", $data_separator = ",") { 
    // Let's seperator each row of data. 
    $separate = explode($row_separator, $db); 

    // First line is always the table column name: 

    $table_columns = 
    $table_rows = array(); 

    foreach ($separate as $key => $row) { 
     // Now let's get each column data out. 
     $data = explode($data_separator, $row); 

     // I always assume the first $row of data contains the column names. 
     if ($key == 0) 
      $table_columns = $data; 
     else if ($key > 0 && count($table_columns) == count($data)) // Let's just make sure column amount matches. 
      $table_rows[] = array_combine($table_columns, $data); 
    } 


    // Return an array with columns, and rows; each row data is bound with it's equivalent column name. 
    return array(
     'columns' => $table_columns, 
     'rows' => $table_rows, 
    ); 
} 

function arrayToDb(array $db, $row_separator = "\n", $data_separator = ",") { 
    // A valid db array must contain a columns and rows keys. 
    if (isset($db['columns']) && isset($db['rows'])) { 
     // Let's now make sure it contains an array. (This might too exagerated of me to check that) 
     $db['columns'] = (array) $db['columns']; 
     $db['rows'] = (array) $db['rows']; 

     // Now let's rewrite the db string imploding columns: 
     $returnDB = implode($data_separator, $db['columns']).$row_separator; 

     foreach ($db['rows'] as $row) { 
      // And imploding each row data. 
      $returnDB .= implode($data_separator, $row).$row_separator; 
     } 

     // Retunr the data. 
     return $returnDB; 
    } 

    // Faaaaaaaaaaaail ! 
    return FALSE; 
} 

我們只是指出我想這與你的分貝例如,當在它自己的結果,如測試它的作品甚至:dbToArray(arrayToDb(dbToArray()))多次。

希望有所幫助。如果我可以更清楚,不要猶豫。 :)

乾杯,

相關問題