titanic_fanataic has asked for the wisdom of the Perl Monks concerning the following question:

I posted this question a few days ago, but I'm just not understanding what it is that I need to do, so I'll explain in more detail what I'm trying to accomplish, also, you can see what I have for code if that helps any HERE.

What I'm doing is writing a script that will allow me the administrator to log in through a web based login(I don't like the look of .htaccess dialogue) and be able to create an account for my clients to login to see their current invoice also with the ability to change their password(because I will set up the original password for their first time login) and also the ability to log out of their account and expire the session. The way that I want the invoice to be shown is by the script "cit.cgi" by extracting the information that I have entered through web form in the admin section from a database where the delimiter is "|".

Here is where my problem is... In the admin section, I want to be able to delete a client. This will bee done by retrieving the database info and displaying each user in a drop down menu(I know how to do this). When I select the user I want it to delete only that entry just by the username(because there won't be any duplicates).

I know that everyone is saying to use MYSQL, and I would but on my server I have access to 1 MYSQL database, and 1 PostgreSQL database,and I've already used the MYSQL database, but I do have 1 PostgreSQL database that I can still use... Although, I'm not really advanced enough in perl to understand how these databases work or even how to use them.

I'm sure that using this kind of database would solve my other problem which is to allow the user to log off through a link in their account.

My concern with this is that my script, as far as I can see will need to use at least 2 flat file databases plus a flat file database that would be created for each user to store their invoice information. I'm not sure that I would be able to fit all of this into the 1 PostgreSQL database that I have left.

If it is possible, could someone explain to me how to do this, and if not how would I go about doing this through my current flat file databases?

Please be as descriptive as possible as this is the first programming language that I'm trying to learn(except for markup language which isn't even really programming)

Thanx a bunch,
You guys have been so helpful so far with my lack of information and experience...

Tylor

Replies are listed 'Best First'.
Re: delete user from database
by diotalevi (Canon) on Dec 09, 2004 at 04:04 UTC

    I wouldn't even imagine of recomending that you use MySQL. While it seems like you're in zero danger of hitting MySQL's ceiling, there's a lot of us that have found it worthwhile to not even bother and have been using the featureful PostgreSQL instead. It probably won't matter to you for at least another year, however.

    Each of the databases MySQL and PostgreSQL can contain multiple tables and are normally expected to. So you could have a list of users, a list of currently authenticated sessions, a list of data on your web sites, etc. When you said "database" you really meant "table." You can have lots of tables in each database. Of course, to interact with a database you'll need to have learned some of the SQL language. Since you're doing this over a CGI web connection, you'll also need to learn about CGI. All in all, you're learning at least three languages simultaneously here. I once found the book Learning Perl by Randal Schwartz to be very helpful along with CGI Programming with Perl. Both are available from http://www.oreilly.com for not a lot of dollars.

Re: delete user from database
by jZed (Prior) on Dec 09, 2004 at 02:57 UTC
    A database in this sense is a collection of tables. Your 1 PostgreSQL database should easily hold your three tables and many more. If you want to stick with flatfiles, consider DBD::CSV which can handle "pipe delimited" files just fine and allow you to do the kinds of things you are asking about with the same DBI interface you would use with PostgreSQL.
Re: delete user from database
by punch_card_don (Curate) on Dec 09, 2004 at 06:32 UTC
    OK, Two methods:
    1. Use the Perl Data Base Interface (DBI) module
      This is a Perl module that lets you write database code in standard SQL regardlesss of the actual database you are using, including flat-files. You'll pretend you're using an actual rdbms, write your code in sql, and DBI will take care of translating that into the commands that will interface with your flat-file. A good introduction to the Perl DBI is http://www.perl.com/pub/a/1999/10/DBI.html. Then, if you decide later on to switch to a real db, you'll have just a few lines of your code to change as you tell DBI which type of db to interface with, instead of the whole thing to re-write. All this work on DBI's part does come at a price - it adds a layer of interpretation and slows things down a bit. But you won't notice it witha flat-file until your user list approaches 1,000 or so rows.

    2. Raw Perl. Here you go:
    Suppose your user "database" is a flat-file that looks like this:
    username|password|fname|lname|user_info
    Your html form in which you select the user to delete is going to have a drop down:
    <form action="myscript.pl"> <select name="user"> <OPTION VALUE="uname_1">user name 1 <OPTION VALUE="uname_2">user name 2 </select> </form>
    Then your Perl script is going to look like:
    # use the CGI module, this automates processing of form input and take +s care of a lot of security issues too use CGI; my $query = new CGI; $user_to_delete = $query->param('user'); #load the user file into an array open(FILE, '<user_data_file.txt'); @user_data = <FILE>; close(FILE); #to find the row with your user, cycle through the rows, parsing each +one and looking for the user name, and write all lines back to the us +er database except the one for the user in question open(FILE, '>user_data_file.txt'); for $i (0 .. $#user_data) { @this_user_info = split(/\|/, $user_data[$i]); if ($this_user_info[0] eq $user_to_delete) { #do nothing } else { print FILE "$user_data[$i]\n"; } } close(FILE);
    This is very basic. You should add code for file locking, for example. And this could could be more elegant with the use of "unless" instead of the if-else.