Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How to match elements of array

by Anonymous Monk
on Dec 03, 2010 at 03:28 UTC ( [id://875072]=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 did ask this question here before, but I am really stuck with this code. I think my question was misunderstood, but I am here trying again please! The user will be uploading a max of 4 pictures from a form in "@pics", but I need to check if any of these picture names in this array is already in the database. And if any pic name in the array "@pics" match any one found in the database I will not allow the code to proceed to do the INSERT.
I just can think about how to do this, here is my code where I am really crazy and stuck, thanks in advance if any one can help!
... if(@pics) # this array has a max of 4 pic names { 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); # I am using this thinking that if $flag_pic returns true there + is already a duplicated pic in the database and # I dont have to do the INSERT my $flag_pic==0; while (my $row = $sth->fetchrow_hashref()) { 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;} } print "<br>**$flag_pic**<br>"; if($flag_pic == 0) # only do this if $flag_pic is not true or equa +l to 0. { 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!!!

Replies are listed 'Best First'.
Re: How to match elements of array
by PeterPeiGuo (Hermit) on Dec 03, 2010 at 03:38 UTC

    Can make this simpler... I have two alternatives:

    1) Create a unique index on file name column, and let the insert fail if it's a duplication.

    2) Use SQL in clause, so use one query, you can figure out whether any of those four names exist.

    Peter (Guo) Pei

Re: How to match elements of array
by samarzone (Pilgrim) on Dec 03, 2010 at 07:42 UTC

    You have already got a better approach from PeterPeiGuo but if you are curious about what's wrong in your code, here are a few things which I noticed

    1. You are not comparing the names, rather you are comparing the existence of names in db and in the array.if(($row->{image_name_1} ne "") eq ($pics[0] ne "")) will increment $flag_pic even if $row->{image_name_1} is "xyz" and $pics[0] is "abc"
    2. (Let us assume previous error is corrected) You are assuming the order to be same in db and in array. If name in first row of db matches with that of second element in the array, it will go unnoticed and you will fire an "insert" query.

    You definitely need to modify the approach

      I am not trying to increment anything, I am trying to assignment a value so it can be true down on the IF statement if the element of the array "@pics" will match or be the same as the one found in the database.
Re: How to match elements of array
by chrestomanci (Priest) on Dec 03, 2010 at 13:19 UTC

    We had a question about Putting 4 items into a DB table last week. Was that you?

    There where a number of useful comments back then, about how it could be done. It would be an idea to read them, even if you where not the supplicant back then.

      Well, I guess I understand what this person is looking for as far as this posting goes, I toke the test code by someone here and added some:
      #!/usr/bin/perl use strict; #use warnings; my @pics; my $test_do_insert = 0; #change value to 0 to change variables to test + a case where it should not insert my $image_name_1 = ""; $image_name_1 = "test_image1.jpg" if (!$test_do_insert); # this is +one of the images already in the database $pics[0] = 'test_image1.jpg'; # this is one of the images comi +ng from this form #$pics[0] = 'test1.jpg'; print "\n14-image_name_1=$image_name_1 - pics[0]=$pics[0]\n"; my $image_name_2 = ""; $image_name_2 = "test_image2.jpg" if (!$test_do_insert); # this is + one of the images already in the database $pics[1] = "test_image2.jpg"; # this is one of the images comi +ng from this form #$pics[1] = "test2.jpg"; print "\n20-image_name_2=$image_name_2 - pics[1]=$pics[1]\n"; my $image_name_3 = ""; #$image_name_3 = "test_image3.jpg" if (!$test_do_insert); #$pics[2] = "test3.jpg"; print "\n26-image_name_3=$image_name_3 - pics[2]=$pics[2]\n"; my $image_name_4 = ""; #$image_name_4 = "test_image4.jpg" if (!$test_do_insert); #$pics[3] = "test4.jpg"; print "\n32-image_name_4=$image_name_4 - pics[3]=$pics[3]\n\n\n"; if (@pics) { my $flag_pic = 0; # while (my $row = $sth->fetchrow_hashref()) { $flag_pic += &check_empty($image_name_1, $pics[0]); print "\n39-".$flag_pic."\n"; $flag_pic += &check_empty($image_name_2, $pics[1]); print "\n41-".$flag_pic."\n"; $flag_pic += &check_empty($image_name_3, $pics[2]); print "\n43-".$flag_pic."\n"; $flag_pic += &check_empty($image_name_4, $pics[3]); print "\n45-".$flag_pic."\n"; # } print "\n47**$flag_pic**\n"; # End check if ( $flag_pic == 0 ) { print "\n50 - YES doing insert\n"; } else { print "\n53 - NOT doing insert\n"; } } sub check_empty() { my $img = shift; my $pic = shift; print "\n60-image_name=^$img^ - pics=^$pic^\n"; if ( $img ne "" && $pic ne "" ) { print "\n63-image_name=$img - pics=$pic\n"; if ( $img=~/$pic/ ) # if they match dont do the sql insert { print "\n66-image_name=$img - pics=$pic\n"; return 1; } else { print "\n72-image_name=$img - pics=$pic\n"; return 0; } print "\n75-image_name=$img - pics=$pic\n"; } }

      And thats the idea about what this person was looking for!
      Enjoy!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2024-04-16 11:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found