Forum Moderators: coopster

Message Too Old, No Replies

Save Form Content in Text file

Save

         

benghee

7:57 am on Jun 9, 2008 (gmt 0)

10+ Year Member



Hi all,
I'm currently working on a form that retrieve and store contents into a textfile. I have done the listing, create, and delete.php but get stucked in making update.php. My problem is everytime i edit the form data, it wont overwrite the existing one but it will append to a new next line. Does anyone here can guide me tru this process? Thanks for many help. Here is my code so far:

$any = "@,@";
$filename="mydata.txt"; //sets file to edit
$handle = fopen($filename, 'r+'); //File handle for $filename

if(isset($_POST['Submit'])) {
{
while(1) {
$line = fgets($handle); //read line from file content
if($line == null)break; //if end of file reached then stop reading
anymore
$newcontents = trim(addslashes($_POST['Name'])).$any.addslashes($_POST['Address']).$any.addslashes($_POST['Area'])."\r\n";
$str.= $line; //set file content to a string
}
}

$str.= $newcontents; //append new updated contents to file content
rewind($handle); //set pointer back to beginning.
ftruncate($handle, filesize($filename)); //delete everything in the file.
fwrite($handle, $str);//write everything back to file with the updated contents.
echo '<script>alert("Reseller Updated !");opener.location.reload();</script>';
echo '<script>window.close();</script>'; exit();
fclose($handle);

}

d40sithui

3:21 pm on Jun 9, 2008 (gmt 0)

10+ Year Member



the way you have it set up looks like you are storing the content of the file in $str, adding the $newcontents to it with concatenation, and then rewriting everything to the file. it did overwrite the file, but it also stored the previous content beforehand before overwriting so you get the old stuff and new stuff. this doesnt make sense to what you're trying to do.

benghee

2:29 am on Jun 10, 2008 (gmt 0)

10+ Year Member



Ya, this is why i'm stucking in solving this problem over and over again that i didnt get the code right. Can anyone guide me here? many thanks :)

benghee

5:31 am on Jun 10, 2008 (gmt 0)

10+ Year Member



I rewrite the code again, but same problem still there. The new entry only will append to next line but it wont overwrite the exsisting one. Thanks for looking this up for me.

$any = "@,@";
if(isset($_POST['Submit']))
{
$filename="mydata.txt"; //sets file to edit
$handle = fopen($filename, 'r+'); //File handle for $filename
$newcontent = trim( addslashes($_POST['Name'])) . $any.addslashes($_POST['Address']) . $any.addslashes($_POST['Area']) . "\r\n";
$current_content = '';
while( !feof( $handle ) )
{
$line = fgets( $handle );
// if eof ( end of file then the last line shouldn't be written back!
if( !feof( $handle ) )
{
$current_content .= $line; //set file content to a string
}
}
fclose( $handle );

$current_content .= $newcontent; //append new updated contents to file content
$handle = fopen($filename, 'w+'); //File handle for $filename writing
fwrite($handle, $current_content); //write everything back to file with the updated contents.
fclose($handle);
echo '<script>alert("Reseller Updated !"); opener.location.reload(); </script>';
echo '<script>window.close();</script>';
}

d40sithui

3:20 pm on Jun 10, 2008 (gmt 0)

10+ Year Member



i think i see the problem now.
you have TWO file handles. the first you used "r+" to fopen and then after you retrieve the file information, you fopen it again with "w+" and in the process replacing the old $handle. after you open it again, you write to it with the contents you already saved. try commenting out or removing the second handle assignment which is the 6th to last line.