Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Duplicity Check Help!

by Anonymous Monk
on Dec 02, 2010 at 14:01 UTC ( #874895=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks!
I am working on this code that allows users to upload pictures, to avoid page refreshes And not to have the SQL INSERT duplicating the record I am trying to check if the pictures is already in the database. I am kind of stuck here because of the value of “$flag_pic” been always set to one no matter what. I know I am missing something or it is just lack of sleep. As you can see I check if "@pics" is true first, the max numbers of pics in this array its only 4, but it can be 1 or 2 or 3 pics, its up to the user. As long as one of them are in the database I don’t want allow the insert to happen again. I hope I made my self clear.
Here is the part of the code (sample) I am having this issue:
... if(@pics) { my $dbh = SQLStuff->connect_mysql(); my $sth = $dbh->prepare("select * from test_users where user = ? " +) or &justdie("Can't select from table: ",$dbh->errmsg); $sth->execute($got_user_name); #my (@pic1,@pic2,@pic3,@pic4,$flag_pic); my $flag_pic; while (my $row = $sth->fetchrow_hashref()) { #push @pic1,$row->{image_name_1} || ''; #push @pic2,$row->{image_name_2} || ''; #push @pic3,$row->{image_name_3} || ''; #push @pic4,$row->{image_name_4} || ''; if(($row->{image_name_1} ne "") eq ($pics[0] ne "")){$flag_pic +="1";} if(($row->{image_name_2} ne "") eq ($pics[1] ne "")){$flag_pic +="1";} if(($row->{image_name_3} ne "") eq ($pics[2] ne "")){$flag_pic +="1";} if(($row->{image_name_4} ne "") eq ($pics[3] ne "")){$flag_pic +="1";} } #my $check_pic1 = shift(@pic1); #my $check_pic2 = shift(@pic2); #my $check_pic3 = shift(@pic3); #my $check_pic4 = shift(@pic4); print "<br>751**$flag_pic**<br>"; # End check if($flag_pic ne "1") { my $dbh = SQLStuff->connect_mysql(); my $sth = $dbh->prepare("insert into test_add(image_1,image_2,i +mage_3,image_4,image_loc,user) values(?, ?, ?, ?, ?,?)") or &justdie("Can't add data, please try again later! ",$dbh->e +rrmsg); $sth->execute((@pics, undef, undef, undef, undef)[0..3],$path_l +oc[0],$got_user) or &justdie("Can't select from table: ",$dbh->errmsg +); } } ...
Thanks for looking!

Replies are listed 'Best First'.
Re: Duplicity Check Help!
by sundialsvc4 (Abbot) on Dec 02, 2010 at 14:37 UTC

    undef is not-equal to “an empty string.”

    Therefore, each of your if statements evalues to true, and the variable gets incremented several times.

    Use the defined() function to check for a NULL field value, which comes across as undef.

    Be sure not to use an “empty string” value in a text column, unless “an empty string” is a plausible, non-NULL possibility in your application.   (Unlikely.)

    As for myself, I am not a fan of using the || alternate_value syntax.   Although you might think of it as being a check for NULL, it is in fact a check for anything that evaluates to a false value according to whatever rules Perl may have.   Which is why you see stuff like this in my code:

    do { $$rec{$_} = "foo" unless defined($$rec{$_}) } foreach qw(FIELD1 FIELD2 FIELD3);
    There are many ways to write it, of course, but the idea is clear:   for an arbitrarily-long list of field names, if any of the returned values is undef a specific value is substituted for them.   (Since this is, of course, being applied against the in-memory (not tied) hash returned by DBI::fetchrow_hashref, it does not modify the database in any way.)

    This uses the foreach modifier exactly as described in perldoc perlsyn.

      How would you apply your way to this code to do these checks?
Re: Duplicity Check Help!
by wallisds (Beadle) on Dec 02, 2010 at 15:27 UTC
    I would be interested to see the description of your table 'test_add'.

    But in the meantime here are some of my initial suggestions:
    my $flag_pic; I would define it and turn on warnings (if not already on): use warnings;
    And for the defining part change my $flag_pic; to
    my $flag_pic = 0;
    Also, where you say :
    I think you should take away the quotes and try this please:
    Now if I understand your question correctly you don't want to add any duplicate images into your table and you are checking this based on the value of $flag_pic.

    I notice you are saying if ($flag_pic ne "1") { #insert }
    but you are adding 1 up to 4 times (given the if statements succeed) in your while loop (as shown here so you know what I'm trying to say)
    if ( ($row->{image_name_1} ne "") eq ($pics[0] ne "") ) { $flag_pic += "1"; } if ( ($row->{image_name_2} ne "") eq ($pics[1] ne "") ) { $flag_pic += "1"; } #and all the way to image_name_4
    ... so if you have two cases where the if succeeds, $flag_pic will be set to 2, not 1, and if you have three, $flag_pic = 3, and so forth. So in any case where more than one if-statement succeeds in your while loop $flag_pic will NOT EQUAL 1.

    I recommend changing your if-statement at the bottom to
    if ($flag_pic == 0) { #insert }

    See if any of that helps.
      Well, yes, I am trying to see if any of the images are already in the database, and if I find any match I am setting the value of "$flag_pic" to something, that way I know that a match was found and I don't have to run the INSERT part.

        Right but what I'm trying to say is your insert code will run anytime $flag_pic is NOT EQUAL to '1' and in your while loop you are incrementing $flag_pic by 1 anytime the image name and pic exist.
        This is done four times in the while loop, which is fine, except in your IF (for inserting) you are saying if (flag_pic ne '1'). You need to check if ($flag_pic == 0) because $flag_pic is not equal to 1 if it equals 0 (which is what you are looking for) but it's also not equal to 1 if it equals 2,3 or 4 ... etc. Does that make sense?
        Try updating the code and see if it helps please.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://874895]
Approved by jonnyfolk
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2023-03-27 20:16 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (65 votes). Check out past polls.