in reply to Re: mysql database
in thread mysql database

I found how to do it but it's printig the values that are in the database twice:once checked and once unchecked.I don't know how to print them only once as checked
for (my $i=0; $i<=$#id; $i++){ my $sth=$dbh->prepare("SELECT Data,Identifier,User,Version FROM ddts,d +elivery WHERE `User`=\"@user\" AND `Identifier`=\"$id[$i]\" AND deliv +ery.Data=ddts.Data"); $sth->execute(); while (my ($data,$identifier,$user,$version)=$sth- +>fetchrow_array ) { print "<tr><td><input type=\"checkbox\" name= +\"rt\" CHECKED value=\"$identifier\"> $identifier</td></tr>";} if ($id[$i] ne $identifier){ print "<tr><td><input type=\"checkbox\" name=\"rt\ +" value=\"$id[$i]\"> $id[$i]</td></tr>"; } + $sth->finish; }
Thank you very much

Replies are listed 'Best First'.
Re: Re: Re: mysql database
by Sandy (Curate) on Dec 18, 2003 at 16:59 UTC
    Maybe(?) your query match to $identifier has extra spaces?

    I will be willing to help you solve this if you want (later this week, not now), but I would need to know a bit more info.

    Could you give me a sample of the input from the db (not the whole thing please), explain why you are querying based on 'user'=\"@user\", print out the select statement before querying, and print the results (as is) of each row fetched from the database.

    It's possible that just doing this may help you solve the problem yourself.

    UPDATE ... ignore above

    Silly me, over lunch I realized what was wrong (at least I think that this is the problem).

    When you query the database, you use a while loop. When a row is returned, you print the resulting CHECKED box. So far so good.

    But, still within the while loop, a new query is performed, and there is no row returned. Now the $identifier is 'undef', and the while loop is exited.

    So what happens next? The if statement is executed, and $identifier (which is 'undef' because of the last query to the db) is not equal to $id. The condition is true, and the unchecked box is printed.

    There are certainly a lot better ways of doing this than what I am about to suggest, (it's a quick and dirty fix) but it should work.

    PS: Note that I used a placeholder, which many people have mentioned in the other posts.
    PSS: I don't have a db to play with, so I think that this works, but I actually haven't checked the syntax of the execute statement. Please check before using!

    my $sth=$dbh->prepare( "SELECT Data,Identifier,User,Version FROM ddts, ". "delivery WHERE `User`=\"@user\" AND `Identifier`= ?" . " AND delivery.Data=ddts.Data"); foreach $id (@id){ $sth->execute($id); my $found = 0; while (my ($data,$identifier,$user,$version)=$sth->fetchrow_array ) { print "<tr><td><input type=\"checkbox\" name=\"rt\" CHECKED ". "value=\"$identifier\"> $identifier</td></tr>"; $found = 1; } unless ($found){ print "<tr><td><input type=\"checkbox\" name=\"rt\" ". "value=\"$id[$i]\"> $id[$i]</td></tr>"; } } $sth->finish;