
DSelver
Novice
Nov 7, 2000, 1:21 PM
Post #1 of 8
(505 views)
|
|
pulling hair out trying to replace data in a flatfile!!
|
Can't Post
|
|
How does one replace ONE line in a flat file database with updated information!!! I start with a comma delimitted text file with the following fields. ordernumber,price,description,id,emailaddr,name,address1,address2,city,st,zip,tracking,insurance. The initial web page allows the user to enter in either the ordernumber, their ID, or their email addres. Once entered the cgi script will open the flatfile and pull the info and populate an HTML form with certain information. The user then adds in their shipping info and checks whether they want tracking or insurance and submits it. So, now that I have this data, how do I write it back to the file! HECK! I can't even write it back to the screen!!! Can one read a file one line at a time and then replace it? or am I going to have to read in the entire file and write the ENTIRE file back out. here is a snippet of how I'm trying to write the data back out to the database file. I've tried so many things that I don't even know where I've left off! I think I'm missing at least a '>' when I do a print OUTF... HELP! <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> # Assign the variable $wid to equal the value of the # web $form(id) key $wid = $FORM{'id'}; #open the file or terminate if error open(INF,"test.txt") or dienice("Couldn't open auctions database for reading: $! \n"); #put the file into an array @data = <INF>; #close the file close(INF); #break the data into lines and begin loop foreach $line(@data) { chomp $line; ($itemnum,$price,$desc,$id,$emailaddr,$name,$addr1,$addr2,$city,$st,$zip,$tracking,$insurance) = split(/\,/,$line); #compare the web form ID with the id field in the database to locate a match if ($wid eq $id) { open(INF,"test.txt") or dienice("Couldn't open auctions database for reading: $! \n"); while (<INF> ) { chomp; ($itemnum,$price,$desc,$id,$emailaddr,$name,$addr1,$addr2,$city,$st,$zip,$tracking,$insurance) = split(/\,/,$line); #got sick of failing! attempting to PRINT the current database line to the browser # to see if it's even working print "$itemnum,$price,$desc,$id,$emailaddr,$name,$addr1,$addr2,$city,$st,$zip,$tracking,$insurance"; } close(INF); # open(OUTF,"> test.txt") or dienice( "Can't open database for writing!"); #lock the database file. # flock(OUTF,2); #send the output back to the file. #print the UPDATED line to the browser to see if the updates are even there! print "$itemnum,$price,$desc,$ebayid,$emailaddr,FORM{'$name'},FORM{'$addr1'},FORM{'$addr2'},FORM{'$city'},FORM{'$st'},FORM{'$zip'},FORM{'$tracking'},FORM{'$insurance'}\n"; #write the file out to disk # print OUT "$itemnum,$price,$desc,$ebayid,$emailaddr,FORM{'$name'},FORM{'$addr1'},FORM{'$addr2'},FORM{'$city'},FORM{'$st'},FORM{'$zip'},FORM{'$tracking'},FORM{'$insurance'}\n"; # close(OUT); #end of sending output to the file. </pre><HR></BLOCKQUOTE>
|