phpaddict Posted January 5, 2007 Share Posted January 5, 2007 im using PHP4.3 on WIN XP. (installed with the windows installer)i made this form handler page. its supposed to get information from a form through $_POST, and then append it to a file, and then read and display the updated information.The writing part is okay,But the problem is this: it reads the previous information, not the latest updated one. Since it writes BEFORE it reads, the informaiton should be updated with the new post, right?But it only displays the old contents of the file, without the latest update (the one last appended).what am i doing wrong?[code]<html><body><?phpglobal $name,$email,$info;$name = "";$email = "";$quote = "";$name = $_POST['name'];$email = $_POST['email'];$info= $_POST[info];WriteInfo($name,$email,$info);ReadInfo();// THIS FUNCTION WRITES THE INFO TO THE FILEfunction WriteInfo($n,$e,$i) {$filename = "d:\scripts\one.txt"; if(file_exists($filename)) { $fs = fopen($filename, "a"); fputs($fs,$n."*".$e."*".$i."**"); fclose($fs); } else { print "Error: file does not exist at d:\scripts\one.txt"; } } // THIS FUNCTION READS THE INFO FROM THE FILEfunction ReadInfo() {$filename = "d:\scripts\one.txt";touch($filename); if(file_exists($filename)) { $fs = fopen($filename,"r"); $output = fgets($fs,filesize($filename)); print "The Info is:"; print "<br>$output"; } else { print "file does not exist at d:\scripts\one.txt"; } } ?></body></html>[/code] Link to comment https://forums.phpfreaks.com/topic/32956-a-file-readwrite-problemplease-help/ Share on other sites More sharing options...
kenrbnsn Posted January 5, 2007 Share Posted January 5, 2007 Please modify your post to put the [nobbc][code] tag before your code and [/code][/nobbc] after it. This will make your code much more readable.Ken Link to comment https://forums.phpfreaks.com/topic/32956-a-file-readwrite-problemplease-help/#findComment-153462 Share on other sites More sharing options...
phpaddict Posted January 5, 2007 Author Share Posted January 5, 2007 ok ken i modified my post thanks :)i have another question too:why is installing php 5.x manually WITHOUT the windows installer so important? Link to comment https://forums.phpfreaks.com/topic/32956-a-file-readwrite-problemplease-help/#findComment-153463 Share on other sites More sharing options...
kenrbnsn Posted January 5, 2007 Share Posted January 5, 2007 I made a test version of your problem script:[code]<?php$name = "";$email = "";$quote = "";if (isset($_POST['submit'])) { $name = $_POST['name']; $email = $_POST['email']; $quote = $_POST['quote']; WriteInfo($name,$email,$quote); ReadInfo();}else { echo '<form method="post">'; echo 'Name: <input name="name" type="text"><br>'; echo 'Email: <input name="email" type="text"><br>'; echo 'Quote: <input name="quote" type="text"><br>'; echo '<input type="submit" name="submit"></form>'; }// THIS FUNCTION WRITES THE INFO TO THE FILEfunction WriteInfo($n,$e,$q) { $filename = "scripts/one.txt"; if(file_exists($filename)) { $fs = fopen($filename, "a"); fwrite($fs,$n."*".$e."*".$q."**\r\n"); fclose($fs); } else { print "Error: file does not exist at d:\scripts\one.txt"; }}// THIS FUNCTION READS THE INFO FROM THE FILEfunction ReadInfo() { $filename = "scripts/one.txt"; touch($filename); if(file_exists($filename)) { $output = file($filename); print "The Info is:<br>"; foreach ($output as $line) echo nl2br($line); } else { print "file does not exist at scripts/one.txt"; }} ?>[/code]I changed the fputs/fgets to fwrite and the file() functions.Ken Link to comment https://forums.phpfreaks.com/topic/32956-a-file-readwrite-problemplease-help/#findComment-153520 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.