CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Help with Testing field in database!

 



CuzDesign
Deleted

Jun 28, 2000, 11:40 PM

Post #1 of 7 (1055 views)
Help with Testing field in database! Can't Post

Hello,
I have been at this for awhile now and just
cannot seem to find out where the problem is.

What I am trying to do is test a record in
a databse if it exsists print one thing if
it doesn't then go ahead and add the new record. This seemed to work fine before however
it seems to not work anymore and I just cannot
seem to find the problem here.

What I am doing is there is a form that is
filed out and I am trying to test one of the
fields in the form with the second part of
the record in the databse $list[1] but for
some reason it is not testing ever record
before it decides to add or not. It just keeps
adding the record no matter what.

Here is the code I am using now. If you can
see where the problem is please let me know.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


my $line;
open (DATA,"<$trucks") or die "Couldn't open $trucks $!\n";
while (<DATA> ){
chomp;
my @list = split(/:/,$_);
if ($list[1] eq $stock){
$good = 1;
}
}
close (DATA);
if ($good == 1) {
print "Stock number already added<br>\n";
print "<a href=\"Javascript:history.back()\">Click here</a> to go back and try a new Stock number\n";
}else{
open (OUTPUT, ">> $trucks");
print OUTPUT $nocamera, ":", $stock, ":", $year, ":", $make, ":", $model, ":", $price, ":", $color, ":", $sedan, ":", $trans, ":", $miles, ":", $desc, "\n";
close (OUTPUT);
print "Vehicle added succesfully\n";
}
</pre><HR></BLOCKQUOTE>

Thanks in advance!
Ray




Jasmine
Administrator

Jun 29, 2000, 4:15 AM

Post #2 of 7 (1056 views)
Re: Help with Testing field in database! [In reply to] Can't Post

Is $stock a number, text, or both?

If $stock is a number, you should use if
($list[1] == $stock), not eq. eq is the
text comparison operator and == is the
numeric comparison operator.

If $stock is text, is it possible that the
user is entering a different case than the
one that is saved in the database. For
example, HUH doesn't equal huh, Huh, HuH,
etc. You may wish to try if (lc($list[1])
eq lc(stock)), which will force the two
items you're comparing to lower case before
attempting the comparison.

The other thing... It looks like you're
joining your fields by a colon and a
space, but splitting only on a colon. This
will make "12345" " 12345", which won't
match the form input.

On a tangent, there are 2 lines that can be cleaned up:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


while (<DATA> ){
chomp;
my $dbstock = (split(/: /))[1]; # 1.
if ($dbstock eq $stock){ # 2
$good = 1;
}
}</pre><HR></BLOCKQUOTE>

1. Because you're already using $_, there's
no need to explicitely note it... by
default, if you're performing an operation
without explicitely specifying a variable,
the operation is performed on $_. Also,
because you're only needing one element from
the line, you don't need to assign the
entire line to an array, then use that array
element. You can just split $_ and assign
the single element you need to directly to a
scalar.

2. Change eq to == if you're comparing
numbers, or use (lc($dbstock) cmp lc
($stock)).

Next...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


print OUTPUT $nocamera, ":", $stock, ":", $year, ":", $make, ":", $model, ":", $price, ":", $color, ":", $sedan, ":", $trans, ":", $miles, ":", $desc, "\n";</pre><HR></BLOCKQUOTE>

can be made cleaner by using:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


print OUTPUT join(": ", $nocamera, $stock, $year, $make, $model, $price, $color, $sedan, $trans, $miles, $desc);
print OUTPUT "\n";</pre><HR></BLOCKQUOTE>

When using the join function, you only need
to define your delimiter once. The rest of
the list (note, it's separated by commas
just like an array) will then be delimited
by the defined delimiter. This also makes
it easy to update the delimiter if need be.




[This message has been edited by Jasmine (edited 06-29-2000).]


CuzDesign
Deleted

Jun 29, 2000, 11:48 AM

Post #3 of 7 (1055 views)
Re: Help with Testing field in database! [In reply to] Can't Post

Jasmine,
Thank you for your fast reply. Thank you for
the suggestions on the changing of the code
to add to the database, this will definitly
make it alot easier to change the delimitor
if need be.I tried what
you suggested but it still does the samething.
No matter if the stock number exsists or not it
is adding it to the database.

To give you a better example of what the
comparision is to here is how the database is
layout.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


camera.gif:P12345:2000:Honda:CiviC:18,999:White:Coupe:5-spd:15:This is one nice car
none:R12345:2000:Honda:CiviC:18,999:White:Coupe:5-spd:15:This is one nice car
camera.gif:T12345:2000:Honda:CiviC:18,999:White:Coupe:5-spd:15:This is one nice car
</pre><HR></BLOCKQUOTE>

and so on for the database. The field I am testing
against is the secong one which is the stock
number of the vehicle. It can be a cobination
of both text and/or numbers. Maybe this
is where I cannot find the problem since it has
both text and numbers. Here is the code as you
had it here I tried:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$good = 0;
open (DATA,"<$trucks") or die "Couldn't open $trucks $!\n";
while (<DATA> ){
chomp;
my $dbstock = (split(/:/))[1]; # 1.
if ($dbstock eq $stock){ # 2
$good = 1;
last;
}
}
close (DATA);
if ($good == 1) {
print "Stock number already added<br>\n";
print "<a href=\"Javascript:history.back()\">Click here</a> to go back and try a new Stock number\n";
}else{
open (OUTPUT, ">> $trucks");
print OUTPUT join(":", $nocamera, $stock, $year, $make, $model, $price, $color, $sedan, $trans, $miles, $desc);
print OUTPUT "\n";
close (OUTPUT);
print "Vehicle added succesfully\n";
}
</pre><HR></BLOCKQUOTE>

I know there has to be something I am doing wrong. There is No space in the database
before or after each entry.

Please let me know if there is something I am
just missing here.

Thank you in advance.
Ray


[This message has been edited by CuzDesign (edited 06-29-2000).]


Jasmine
Administrator

Jun 29, 2000, 12:07 PM

Post #4 of 7 (1055 views)
Re: Help with Testing field in database! [In reply to] Can't Post

Okay... how is $stock being assigned? That is, how are you reading form input?

Next, are you certain someone isn't typing r12345 instead of R12345? There's still no checking on case.

Also, for testing purposes only, try changing:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


while (<DATA> ){
chomp;
my $dbstock = (split(/:/))[1]; # 1.
if ($dbstock eq $stock){ # 2
$good = 1;
last;
}
}
</pre><HR></BLOCKQUOTE>

to:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


while (<DATA> ){
chomp;
my $dbstock = (split(/:/))[1]; # 1.
if ($dbstock eq $stock){ # 2
$good = 1;
last;
}
else {
print "[$dbstock] ne [$stock]<BR>\n";
}
}
</pre><HR></BLOCKQUOTE>

This will help you identify exactly what the reason is.

Hope this helps!


CuzDesign
Deleted

Jun 29, 2000, 12:34 PM

Post #5 of 7 (1055 views)
Re: Help with Testing field in database! [In reply to] Can't Post

Jasmine,
Thank you again for your fast reply. I tried
changing it to make it lower case using the lc
and still get the same problem.

Let me explain it better as to where everything
is coming from. I am getting the input from a
text field in a form like so:

$stock = param('stock');

as well as the other fields that are in the form. What I have is 2 subs one for testing
one field to see which database to write to.
Here is the entire program as it sits now.
Like so:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


#!/usr/local/bin/perl

use CGI qw(param);
print "Content-type: text/html\n\n";

####################################
##### Start User configuration #####
####################################

# Name of domestic database file for cars
$domestic = "domestic.dat";

# Name of domestic database file for cars
$imports = "imports.dat";

# Name of domestic database file for cars
$trucks = "trucks.dat";

# Your path to where you want your files uploaded.
# Note: NO trailing slash
$upload = '/home/cuzzart/cuzzart-www/usedhtml/images';

# Path to details html files
$path = '/home/cuzzart/cuzzart-www/usedhtml';

# Do you wish to allow all file types? yes/no (no capital letters)
$allowall = "no";

# If the above = "no"; then which is the only extention to allow?
# Remember to have the LAST 4 characters i.e. .ext
$theext = ".gif";

# Add picture camera.gif to database if image uploaded
$pic = "camera.gif";

# If added file has no image set database to no pic
$nocamera = "none";

# Name and location of header.html file
$head = "html.header";

# Name and location of footer.html file
$foot = "html.footer";

####################################
##### End User configuration #####
####################################


#######################################################################
#################### DO NOT EDIT BELOW THIS LINE ######################
#######################################################################

# Set Header
local $/;
open(HEAD, "<$head") or die $!;
$header = <HEAD>;
close HEAD;

# Set footer
local $/;
open(FOOT, "<$foot") or die $!;
$footer = <FOOT>;
close FOOT;

## Get contents of form and set to variables
$stock = param('stock');
$year = param('year');
$make = param('make');
$model = param('model');
$price = param('price');
$color = param('color');
$sedan = param('sedan');
$trans = param('trans');
$miles = param('miles');
$type = param('type');
$desc = param('des');

# Test to see what the action is and do sub accordingly
$action = param('action');
if ($action eq ''){&form}
elsif ($action eq 'main'){&main}
elsif ($action eq 'auth'){&auth}
elsif ($action eq 'show'){&show}
elsif ($action eq 'add'){&add}

sub form {
print $header;
print <<EOF;
<tr>
<td><img src="http://cuzzart.com/used/images/add_vehform_top.gif" width="525" height="30" border="0"></td>
</tr>
<tr>
<td bgcolor="#C0C0C0"><FONT SIZE="-1" FACE="verdana, arial, helvetica">Use the form below to add a vehicle to your inventory database.
Vehicles added will be visable immediately. If you do not have an image leave the image field <b>BLANK</b>.</font></td>
</tr>
</table>
<table width="525" cellspacing="0" cellpadding="4" border="0" bgcolor="#C0C0C0">
<tr><form action="used.pl" method="post" ENCTYPE="multipart/form-data">
<input type=\"hidden\" name=\"action\" value=\"add\">
<td><FONT SIZE="-1" FACE="verdana, arial, helvetica"><b>Stock #:</b></font></td>
<td align="left"><input type="text" name="stock" size="25" maxlength="45"></td>
</tr>
<tr>
<td><FONT SIZE="-1" FACE="verdana, arial, helvetica"><b>Year:</b></font></td>
<td align="left"><input type="text" name="year" size="25" maxlength="55"></td>
</tr>
<tr>
<td><FONT SIZE="-1" FACE="verdana, arial, helvetica"><b>Make:</b></font></td>
<td align="left"><input type="text" name="make" size="25" maxlength="35"></td>
</tr>
<tr>
<td><FONT SIZE="-1" FACE="verdana, arial, helvetica"><b>Model:</b></font></td>
<td align="left"><input type="text" name="model" size="25" maxlength="35"></td>
</tr>
<tr>
<td><FONT SIZE="-1" FACE="verdana, arial, helvetica"><b>Price:</b></font></td>
<td align="left"><input type="text" name="price" size="25" maxlength="55"></td>
</tr>
<tr>
<td colspan=2 bgcolor="#000000"><FONT SIZE="-1" FACE="verdana, arial, helvetica" color="#FFFFFF"><b>Vehicle Details</b></font></td>
</tr>
<tr>
<td><FONT SIZE="-1" FACE="verdana, arial, helvetica"><b>Color:</b></font></td>
<td align="left"><input type="text" name="color" size="25" maxlength="45"></td>
</tr>
<tr>
<td><FONT SIZE="-1" FACE="verdana, arial, helvetica"><b>Sedan/Coupe:</b></font></td>
<td align="left"><input type="text" name="sedan" size="25" maxlength="55"></td>
</tr>
<tr>
<td><FONT SIZE="-1" FACE="verdana, arial, helvetica"><b>Transmission:</b></font></td>
<td align="left"><input type="text" name="trans" size="25" maxlength="35"></td>
</tr>
<tr>
<td><FONT SIZE="-1" FACE="verdana, arial, helvetica"><b>Miles:</b></font></td>
<td align="left"><input type="text" name="miles" size="15" maxlength="15"></td>
</tr>
<tr>
<td><FONT SIZE="-1" FACE="verdana, arial, helvetica"><b>Type:</b></font></td>
<td align="left"><select name="type" size="1">
<option value="dom" SELECTED>Domestic </option>
<option value="imp">Import </option>
<option value="tru">Truck </option>
</select></td>
</tr>
<tr>
<td><FONT SIZE="-1" FACE="verdana, arial, helvetica"><b>Description:</b></font></td>
<td align="left"><textarea cols="35" rows="5" name="des"></textarea></td>
</tr>
<tr>
<td><FONT SIZE="-1" FACE="verdana, arial, helvetica"><b>Image:</b></font></td>
<td align="left"><input type="file" name="FILE1" size="25"><br><FONT SIZE="-1" FACE="verdana, arial, helvetica"><i>(If you do not have an image leave this blank)</i></font></td>
</tr>
<tr>
<td align="right"><input type="Reset"></td>
<td align="center"><input type="submit" value="Add To Inventory"></td>
</tr>
</form>
</table>
<table width="416" cellspacing="0" cellpadding="0" border="0">
<tr>
<td><img src="http://cuzzart.com/used/images/add_vehform_bottom.gif" width="525" height="28" border="0"></td>
</tr>
<tr>
<td align="center"><br><a href="Javascript:history.back()">Back</a></td>
</tr>
EOF
print $footer;
}

# Add information to Database once form has been filled in
sub add {
$stock = param('stock');
# If Truck add vehicle to truck database
if ($type eq "tru"){&trucks;}
elsif ($type eq "dom"){&domestic;}
elsif ($type eq "imp"){&imports;}
}
# Add to Trucks database
sub trucks{
$good = 0;
open (DATA,"<$trucks") or die "Couldn't open $trucks $!\n";
while (<DATA> ){
chomp;
my $dbstock = (split(/:/))[1]; # 1.
if (lc($dbstock) eq lc($stock)){ # 2
$good = 1;
last;
}else{
print "[$dbstock] ne [$stock]<BR>\n";
}
}
close (DATA);
if ($good == 1) {
print "Stock number already added<br>\n";
print "<a href=\"Javascript:history.back()\">Click here</a> to go back and try a new Stock number\n";
}else{
open (OUTPUT, ">> $trucks");
print OUTPUT join(":", $nocamera, $stock, $year, $make, $model, $price, $color, $sedan, $trans, $miles, $desc);
print OUTPUT "\n";
close (OUTPUT);
print "Vehicle added succesfully\n";
}
}

# Add to domestic database
sub domestic{
$good = 0;
open (DATA,"<$domestic") or die "Couldn't open $domestic $!\n";
while (<DATA> ){
chomp;
my $dbstock = (split(/:/))[1]; # 1.
if (lc($dbstock) eq lc($stock)){ # 2
$good = 1;
last;
}else{
print "[$dbstock] ne [$stock]<BR>\n";
}
}
close (DATA);
if ($good == 1) {
print "Stock number already added<br>\n";
print "<a href=\"Javascript:history.back()\">Click here</a> to go back and try a new Stock number\n";
}else{
open (OUTPUT, ">> $domestic");
print OUTPUT join(":", $nocamera, $stock, $year, $make, $model, $price, $color, $sedan, $trans, $miles, $desc);
print OUTPUT "\n";
close (OUTPUT);
print "Vehicle added succesfully\n";
}
}

# Add to Imports database
sub imports{
$good = 0;
open (DATA,"<$imports") or die "Couldn't open $imports $!\n";
while (<DATA> ){
chomp;
my $dbstock = (split(/:/))[1]; # 1.
if (lc($dbstock) eq lc($stock)){ # 2
$good = 1;
last;
}else{
print "[$dbstock] ne [$stock]<BR>\n";
}
}
close (DATA);
if ($good == 1) {
print "Stock number already added<br>\n";
print "<a href=\"Javascript:history.back()\">Click here</a> to go back and try a new Stock number\n";
}else{
open (OUTPUT, ">> $imports");
print OUTPUT join(":", $nocamera, $stock, $year, $make, $model, $price, $color, $sedan, $trans, $miles, $desc);
print OUTPUT "\n";
close (OUTPUT);
print "Vehicle added succesfully\n";
}
}
</pre><HR></BLOCKQUOTE>

Here is what I am getting when I added that else
staement you put in:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


[G12345] ne [R12345]
[] ne [R12345]
Vehicle added succesfully
</pre><HR></BLOCKQUOTE>

Which from looking at it tells me it is stopping
after it finds a match but adds as well as when
it finds a match.

Hope this helps explain it better of what I am
trying to do and maybe help find the problem.

Thank you for all your help.

Thank you,
Ray




[This message has been edited by CuzDesign (edited 06-29-2000).]


Jasmine
Administrator

Jun 29, 2000, 3:41 PM

Post #6 of 7 (1055 views)
Re: Help with Testing field in database! [In reply to] Can't Post

Methinks we're getting closer now Smile

Assuming you have more than one line in your
data file, the problem starts when setting
your header and footer variables.

$/ is the record input separator, and by
default it's a newline. When you open your
header/footer file, you use:

local $/;

Using local $/ undefines the default record
separator and slurps the entire multiline
header/footer file into a variable, but it's
also affecting the rest of the program from
that point down.

What happens when a data file whose records are delimited by newlines loses the newlines?

If forces the entire data file to behave
like a single line. If $stock is P12345,
and P12345 is the 4th record, P12345 is not
located at $list[1], it's located at $list[13].

You can do one of two things:

1) Encapsulate the header/footer assignments

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


{
# Set Header
local $/;
open(HEAD, "<$head") or die $!;
$header = <HEAD>;
close HEAD;

# Set footer
open(FOOT, "<$foot") or die $!;
$footer = <FOOT>;
close FOOT;
}
</pre><HR></BLOCKQUOTE>

The local assignment to $/ will lose scope after it exits the block.

Or, reset the $/ (record input separator) back to a newline after you assign the header/footer.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


local $/ = "\n";
</pre><HR></BLOCKQUOTE>

Hope this helps!


CuzDesign
Deleted

Jun 29, 2000, 4:22 PM

Post #7 of 7 (1055 views)
Re: Help with Testing field in database! [In reply to] Can't Post

Jasmine,
Thank you very much for the help. I did
what you said by Encapsulating the header
and footer assignments and everything worked
great. Now all is working well.

Again thank you for your help. Smile Smile

Regards,
Ray

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives