如何使用 php 替换 CSV 文件中一行中的 1 个值


How do I replace 1 value within a row in a CSV file using php?

所以这是我非常简单和基本的帐户系统(只是一个学校项目),我希望用户能够更改他们的密码。但是我不确定如何在保持所有其他值相同的行中替换密码值。

CSV 文件:

ID,Username,Email,DateJoined,Password,UserScore,profilePics
1,Test,Test@Test.com,03/12/2014,Test,10,uploads/profilePics/Test.jpg
2,Alfie,Alfie@test.com,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!");
}
?>

您必须在读取模式下打开文件,在写入模式下打开临时文件,在那里写入修改后的数据,然后删除/重命名文件。我建议尝试设置一个真正的数据库并使用它,但如果你要使用 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

希望对您有所帮助。

我的建议是我的一个小函数,它将把你的数据库数据变成一个数组,你可以修改它,然后回到原始状态:

使用这组函数,您只需精确地确定每行/行数据的分离方式。

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())),它也可以工作。

希望有帮助。如果我能说得更清楚,请不要犹豫。:)

干杯

您需要一个 3 步过程来执行此操作(创建 3 个循环,可以优化为 1 或 2 个循环):

  1. 将相关数据加载到内存
  2. 更新所需数据
  3. 将数据保存到文件

祝你好运! :)

附言。此外,您的密码永远不应该以明文形式存储,无论是在内存(会话)还是磁盘(csv)中,请使用hasing功能!