--- "FLAHERTY, JIM-CONT" <[EMAIL PROTECTED]> wrote: > foreach $row(@$array_ref) { > my($num,$title,$media,$serial,$time,$class,$remarks,$custody,$loc, > $format,$qty,$lab,$rew,$sta,$history5,$check) = @$rows; > > $history2 = $history5; > $history2++; > > } > $dbh2 =DBI ->connect($data_source, $username, $password); > my $sth2 = $dbh2 -> prepare("update media1 set history5 ='$history2' where > num = > '$num'");
One problems that I see that no one else appears to have pointed out is the scoping issue. Your $num is falling out of scope and doesn't have the correct value. I would recommend indenting your code (I'm going to shorten the lines in hopes of avoiding wrapping and making the issue clearer): ######################## foreach $row (@$array_ref) { my($num,$serial,$history5,$check) = @$rows; $history2 = $history5 + 1; } $dbh2 = DBI->connect($data_source, $username, $password); $history2 = $dbh2->quote( $history2 ); $num = $dbh2->quote( $num ); $sql = "UPDATE media1 SET history5 = $history2 WHERE num = $num" my $sth2 = $dbh2->prepare( $sql ); ######################## Now the above snippet is functionaly equivant to yours. Aside from shortening the lines for clarity, I also quoted the two variables used in the SQL to avoid possible problems (see perldoc DBI for more info on this). Since they should both be numbers, this probably isn't necessary, but later on if you start changing a lot of stuff, it could be important. The reason indenting is so important is that it shows immediately an issue that was not apparent at first: your $num has fallen out of the scope of the for loop. When it gets to the sql, either it is undef (in which case, your SQL won't update - assuming $num is an ID), or it has retained some value that was defined outside of the scope we see in the current snippet. Also, I can't help but wonder what's intended here. Are there several rows in $array_ref that you want to update? If so, the above snippet will only update the last one. Since you have an array reference, it looks like you meant to update several (otherwise, you wouldn't be using a for loop). Therefore, you could try this: ######################## $dbh2 = DBI->connect($data_source, $username, $password); $sql = "UPDATE media1 SET history5 = ? WHERE num = ?" # always prepare the sql outside of a loop, if possible. my $sth2 = $dbh2->prepare( $sql ); foreach $row (@$array_ref) { my($num,$serial,$history5,$check) = @$rows; $history2 = $dbh2->quote( $history5 + 1 ); $num = $dbh2->quote( $num ); my $rc = $sth2->execute( $history2, $num ); # do something with return code... } ######################## Hope this helps. Cheers, Curtis "Ovid" Poe ===== "Ovid" on http://www.perlmonks.org/ Someone asked me how to count to 10 in Perl: push@A,$_ for reverse q.e...q.n.;for(@A){$_=unpack(q|c|,$_);@a=split//; shift@a;shift@a if $a[$[]eq$[;$_=join q||,@a};print $_,$/for reverse @A __________________________________________________ Do You Yahoo!? Yahoo! - Official partner of 2002 FIFA World Cup http://fifaworldcup.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]