使用php更新文本文件中的记录


Updating records in a text file with php

我使用php来显示从一个文本文件中读取的记录,该文件中有空格填充的字符串,每个字符串的大小为10。我打开了解压缩后的文件,并将其显示在文本框中,直到达到EOF。这是我使用的代码

<html>  
<head>
    <title>Read Records</title>
    <?php 
        $YourFile = "student.txt"; 
        $handle = fopen($YourFile,'r'); 
        $id = 0;
        while (!feof($handle)) {
            if ($s = fread($handle, 30)) {
                $readata = unpack("A10chars/A10int/A10inr",$s);
                $chars = $readata['chars'];
                $int = $readata['int'];
                $inr = $readata['inr'];
                echo "<input type='"text'" id='"chars$id'" name='"name'" value='"$chars'">";
                echo "<input type='"text'" id='"int$id'" name='"name'" value='"$int'">";
                echo "<input type='"text'" id='"inr$id'" name='"name'" value='"$inr'">";
                echo "<br>";
                $id = $id + 1;
            }
        }
    fclose($handle); 
?> 
</head>

现在,如果用户编辑任何记录,我需要跟踪所做的更改,并在文件中进行更新。元素的ID是使用php代码生成的。如何找到正在编辑的记录??

以下是逻辑:

读取文件时,需要记住读取的第一个和最后一个字符的位置。如果用户更新,则需要从第一个到最后一个位置删除文本,然后在其位置插入新文本。

尽管我不确定你会怎么做。

你有没有研究过SQLite(http://www.sqlite.org/),这是一种简单的方法来做你正在尝试做的事情。由于没有安装SQLite,DB文件在你的机器上保持本地。