Time:
Scotland: Wed, 3:04 pm Rhode Island: Wed, 10:04 am Florida: Wed, 10:04 am New Mexico: Wed, 8:04 am California: Wed, 7:04 am
Buy this Ad Space. 180px wide. Please get in touch with KH@ if you are interested and make an offer.
US$10 per year - Save $100s! The Fabulously Unfair WebX Auction Bar. For Ebay etc. Ro-Sham-Bo the opposition. Laugh like Eric Cartman when you win! CLICK HERE NOW!More information and sign-up.
WebXpertz Hosting. Custom fit from $5pm. PHP/MySQL You'll save money, we'll save money. Seems fair to me. Interested? If so Please PM me here and tell me what you need. Thanks!
|
For continued disscussion on this topic : Erase one line in a database
divaone 04-19-2003, 11:27 PM :bambooz: I'm at my wit's end and need just a touch of help with a script please.
My script(s) reads, writes, edits and deletes to/from a flatfile database, piped. My problem is with
the delete subroutine. The information was being deleted but the pipes remained. I know its
more than likely a very simple problem but I cannot figure it out. I am old to scripts and tweaking
but not to writing from scratch at all.
Previously I used the following: open (FILE,">$mydatabase");
@DB=<FILE>;
foreach $rec (@prog){
chomp($rec);
($one,$two,$three,$four)=split(/\|/,$rec);
if ($one eq $input{'one'} && $two eq $input{'two'} && $three eq $input{'three'} && $four eq $input{'four'}){
print FILE "$one|$two|$three|$four\n";
}else{
print FILE "";
}
}
close (FILE); I've also tried this from the tutorial on this site: open(DAT, $database) || die("Cannot Open File");
@raw_data=<DAT>;
close(DAT);
splice(@raw_data,1,1);
open(DAT,">$database") || die("Cannot Open File");
print DAT @raw_data;
close(DAT); ..which works great, 'cept it deletes that last lil line (what its for, but I tried anyway :rolleyes: ).
I tried using 'last' but thats no option, as I need to erase whatever line the user has chosen to erase.
Can someone help me out with coding to erase a specific line in a database please?
Is this enough info? I've spent hours tinkering and searching for help with only minimal success.
unix, latest perl
Thank you in advance,
divaone
Phineus 04-20-2003, 12:19 AM Splice is nice but only if you know with certainty that your file is ordered properly. I'd probably use something like this instead.# read the data into an array
open (DAT," $mydatabase") || die("Cannot Open File");
@DB = <DAT>;
close(DAT);
# open the file for writing. Remember,
# reading and writing are separate processes
open (FILE,">$mydatabase") || die("Cannot Open File");
# loop through the data
foreach $rec (@DB){
chomp($rec);
($one,$two,$three,$four)=split(/\|/,$rec);
# don't write lines that match the input selection
# write everything else
unless ($one eq $input{'one'}) {
print FILE "$one|$two|$three|$four\n";
}
# stop looping
}
# close the file handle
close (FILE);
divaone 04-20-2003, 01:08 AM Ok this is really good information. I see a couple of things I had wrong by studying the code
you posted. I also found another mistake I made in the delete link.
It was :<a href="edit.cgi?action=delete\barurl=$barurl\&one=$one\&two=$two\&three=$three\&four=$four">Delete</a> and is now :<a href="edit.cgi?action=delete=$barurl\&one=$one\&two=$two\&three=$three\&four=$four">Delete</a>Big difference! Don't know what possessed me to place that slash.
Anyway, thank you much for the help. I'll be studying this more tonight.
divaone
Phineus 04-20-2003, 02:24 AM Wouldn't it be
edit.cgi?action=delete\&barurl=$barurl\&one=$one...?
But I want to ask: why are you including all the values in the query line? Do you not have a unique field that identifies each record? What I've done is use column one as an index/id value... that way I can simplify most processes. For example.
edit.cgi?action=delete&thisrecord=$one
edit.cgi?action=modify&thisrecord=$one
If the value of $one is correct, the other columns in the record will be too.
divaone 04-20-2003, 04:57 AM Originally posted by Phineus
Wouldn't it be
edit.cgi?action=delete\&barurl=$barurl\&one=$one...?
But I want to ask: why are you including all the values in the query line? Do you not have a unique field that identifies each record? What I've done is use column one as an index/id value... that way I can simplify most processes. For example.
edit.cgi?action=delete&thisrecord=$one
edit.cgi?action=modify&thisrecord=$one
If the value of $one is correct, the other columns in the record will be too.
Hmm.. does that assume the columns are headers for the database file entries? Like header1, header2, etc. Not sure if I'm stating this correctly, but my datafile will have no header row or column.
Regarding &barurl, there's actually no subroutine by that name. Do you think its unsafe to call the delete action the way I wrote it.. possible causing errors on some servers?
Thanks :)
Phineus 04-20-2003, 05:23 AM The information in each column must be consistent for each row or the information stored in the database is not retrievable in a meaningful way. For example.
| ID | Title | FirstName | LastName | Address | whatever |
| 1 | Mr | Wallace | Stevens | 123 Elm Street | yes |
| 2 | Ms | Emily | Nightengale | 321 Oak Lane | no |
| 3 | Jr | Horatio | Burbank | 456 Wimple Road | maybe |
So in this example, I could say
edit.cgi?action=modify&id=2
Then I could call that information into a page for updating. We know it will be Emily's info because that's what corresponds to record number two. (There is no need to place all her info into the query string - which can lead to errors anyway if one piece of data is wrong to begin with). The only trick is to be sure you don't use duplicate index/id values. But the main point is that all information must be stored consistently across all records. If you mix the address and the last name around, it will affect your output.. since your script depends on knowing what's in each field. Whether you actually label the columns is of no consequence.
>> action=delete=$barurl
Well, I don't know what value $barurl contains but I can say that most form parsers might have problems since they tend to split first on the & sign and then on the = sign. Having two may confuse the results... unless you've written your own function to take it into account. The server isn't really the issue but how you're processing the query string.
vBulletin® v3.6.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.
|