Click to See Complete Forum and Search --> : CGI Script


Primus
03-04-2003, 02:02 AM
Hey, I am new to CGI scripting, and I am having a little bit of trouble. This script partially works it is suppose to modify entries in an already exsisting database. It will write back to the database, but it will be blank. I can't get the script to get to the screen where you modify the entry you selected. Note I am not a scripting wizard, and this script isn't complete. I also haven't commented it very much yet So please don't flame me. I am pretty sure it is a simple error that I have just overlooked. I just wanted some other eyes to look it over.

Thanks.


#!/usr/bin/perl -w

use CGI qq~:standard~;


#Content-type: text/html\n\n;

#Print an HTML header.
print header ();

# The Page Title
print start_html ("Our Model Cars!");

if (request_method() eq "GET")
{
# Open the file products.dat and refer to it as "FILE"

open FILE, "products.dat";

print "<h3> Which Record would you like to modify?</h3>\n";
print "<table>\n";

# Do a while loop, seperate each record in fields 1 and 2 and print them in a table.

while (<FILE>)
{

chomp;
@fields = split /\|/;

print qq~<tr>\n~;
print qq~<td><form method="POST" action="/cgi-bin/update.cgi">\n~;

# Generates a group of radio buttons for fields in the database.
print qq~<td><input type="radio" name="radio" value=@fields>$fields[1] - $fields[2] \n~;

print qq~\<input type="hidden" name="key" value="$fields[0]">\n~;
}

print qq~<P><input type="submit" value="Change">\n~;
print "</table>\n";
}

else
{
print "<h3>An Error has occured while trying to write to database!</h3>\n";
}





if(param('submit') eq "Change")
{
open FILE,"products.dat";

print "<h3>Changing Record</h3>";
print qq~<form method="GET" action="/cgi-bin/update.cgi">\n~;

while (<FILE>)
{
@fields = split /\|/;

if ($fields[0] eq param('key'))
{

print qq~Car: <input type="text" name="car" value="$fields[1]"><BR>\n~;
print qq~Description: <input type="text" name="desc" value="$fields[2]"><BR>\n~;
print qq~Price: <input type="text" name="price" value="$fields[3]"><BR>\n~;
print qq~Picture: <input type="text" name="pic" value="$fields[4]"><BR>\n~;
print qq~<input type="hidden" name="key" value="$fields[0]">\n~;
print qq~<input type="submit" name="submit" value="Save">\n~;

last;
}
}
print "</form>\n";
}

else
{
open FILE, "+<products.dat";
flock (FILE, 2);

open TEMP, ">temp.dat";
flock (TEMP, 2);

while (<FILE>)
{
@fields= split /\|/;

if ($fields[0] eq param('key'))
{

print TEMP param('key') . "|" . param('car') . "|" . param('desc') . "|" . param('price') . "|" . param('pic') . "\n";
}
else
{

print TEMP $_;
}
}

# Save the database with the updated record.
unlink "products.dat";
rename "temp.dat", "products.dat";

print "<h3>Record Saved to Database sucessfully!</h3>";

close TEMP;
}

print end_html();
close FILE;