当循环应该做超过 500 个时,它只对表进行一次插入。我做了很多显示来证明@book_ids 中有超过 500 个条目。但只有第一个条目被插入到表中。
相关脚本代码:
#!/usr/bin/perl
# Script to List Books Database.
#
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);
use DBI;
use strict;
use warnings;
use Text::Autoformat;
require 'appl.lib';
# verify access coming from proper source
my ($source) = &verify_visitor();
###### Establish connection to BookDB package
use BookDB;
my ($bookdb) = new BookDB();
my ($success);
######
#####################################################################
###### connect to the database
my ($dsn) = "DBI:mysql:webstone:localhost"; # data source name
my ($user) = "xxxxxxxxx"; # db user name
my ($password) = "xxxxxxxx"; # db password
my ($DBI) = "";
my (%attr) = # error handling attributes
(
PrintError => 0,
RaiseError => 0
);
my ($SQL, $str); # various handles
my ($db_user, $db_pw); # db returned user_id and pass_word
my ($login); # login verification switch
my ($dbh) = DBI->connect($dsn, $user, $password, \%attr)
|| &bail_out ("Cannot connect to the database");
if(param())
{
#do this for security reasons. It prevents bookmarking URL's with
#passwords and the like encoded in them, and prevents some security hazards.
die "Not a POST request!\n" unless ((request_method() eq "POST") || ($source eq "books"));
}
#####################################################################
my ($book_count, $c_count, $w_count, $s_count, @book_ids, $book_ids, @book, $book, @all_book_data, $all_book_data, $rec, $pass_book_id);
my (@rawdata, $rawdata, @un_x, $un_x, @sorted, $sorted);
### hold Master Book table data in temp table
my ($m_id, $m_book_id, $m_isbn, $m_copyright_year, $m_status, $m_title, $m_alt_title, $m_notes);
my ($m_author_id, $m_author1_name, $m_author2_name, $m_author_book_id);
my ($m_series_id, $m_series_name, $m_series_num, $m_series_book_id);
my ($m_order_id, $m_order_date, $m_due_date, $m_order_book_id);
### hold Book table data when read
my ($h_book_id, $h_isbn, $h_copyright_year, $h_status, $h_title, $h_alt_title, $h_notes);
### hold Author table data when read
my ($h_author_id, $h_last_name, $h_first_name, $h_middle_name, $h_author_book_id);
my ($h_alt_author_id, $h_alt_last_name, $h_alt_first_name, $h_alt_middle_name, $h_alt_author_book_id);
### hold Series table data when read
my ($h_series_id, $h_series_name, $h_series_num, $h_series_book_id);
### hold Order table when read
my ($h_order_id, $h_order_date, $h_due_date, $h_order_book_id);
my ($msg, $content, $page);
my $html_header = "Content-Type: text/html; charset=ISO-8859-1\n\n";
my ($disp_date, $date_store) = &get_today_date();
my ($sent_user) = param("username");
my ($sent_passwd) = param("passwd");
my ($action) = param("function");
my ($direct) = param("direction");
my ($choice) = param("menu_choice");
my ($p_username) = "p_username";
my ($pass_username) = param("p_username");
### Mainline
if ($action eq "list")
{
($success, $c_count) = $bookdb->create_temp_tbl($dbh);
($success, $book_count, @book_ids) = $bookdb->get_book_ids($dbh);
my ($c);
foreach $pass_book_id (@book_ids)
{
($success, $h_book_id, $h_isbn, $h_copyright_year, $h_status, $h_title, $h_alt_title, $h_notes) = $bookdb->get_book_data($dbh, $pass_book_id);
$m_id = $c;
$m_book_id = $h_book_id;
$m_isbn = $h_isbn;
$m_copyright_year = $h_copyright_year;
$m_status = $h_status;
$m_title = $h_title;
$m_alt_title = $h_alt_title;
$m_notes = $h_notes;
($success, $h_author_id, $h_last_name, $h_first_name, $h_middle_name, $h_author_book_id) = $bookdb->get_author1_data($dbh, $pass_book_id);
$m_author_id = $h_author_id;
$m_author_book_id = $h_author_book_id;
$m_author1_name = $h_last_name . ", " . $h_first_name . " " . $h_middle_name;
($success, $h_alt_author_id, $h_alt_last_name, $h_alt_first_name, $h_alt_middle_name, $h_alt_author_book_id) = $bookdb->get_author2_data($dbh, $pass_book_id, $h_author_id);
if ($h_alt_author_id)
{
$m_author2_name = $h_alt_last_name . ", " . $h_alt_first_name . " " . $h_alt_middle_name;
}
else
{
$m_author2_name = "";
}
($success, $h_series_id, $h_series_name, $h_series_num, $h_series_book_id) = $bookdb->get_series_data($dbh, $pass_book_id);
if ($h_series_id)
{
$m_series_id = $h_series_id;
$m_series_book_id = $h_series_book_id;
$m_series_name = $h_series_name;
$m_series_num = $h_series_num;
}
else
{
$m_series_id = "";
$m_series_book_id = "";
$m_series_name = "";
$m_series_num = "";
}
($success, $h_order_id, $h_order_date, $h_due_date, $h_order_book_id) = $bookdb->get_order_data($dbh, $pass_book_id);
if ($h_order_id)
{
$m_order_id = $h_order_id;
$m_order_book_id = $h_order_book_id;
$m_order_date = $h_order_date;
$m_due_date = $h_due_date;
}
else
{
$m_order_id = "";
$m_order_book_id = "";
$m_order_date = "";
$m_due_date = "";
}
($success, $w_count) = $bookdb->write_temp_tbl($dbh, $m_id, $m_book_id,
$m_isbn, $m_copyright_year, $m_status, $m_title, $m_alt_title, $m_notes,
$m_author_id, $m_author1_name, $m_author2_name, $m_author_book_id,
$m_series_id, $m_series_name, $m_series_num, $m_series_book_id,
$m_order_id, $m_order_date, $m_due_date, $m_order_book_id);
}
my ($g);
for ($g = 0; $g < $book_count; $g++)
{
$pass_book_id = $book_ids[$g];
($success, $s_count, $m_id, $m_book_id, $m_isbn, $m_copyright_year, $m_status, $m_title, $m_alt_title, $m_notes,
$m_author_id, $m_author1_name, $m_author2_name, $m_author_book_id,
$m_series_id, $m_series_name, $m_series_num, $m_series_book_id,
$m_order_id, $m_order_date, $m_due_date, $m_order_book_id)
= $bookdb->get_temp_data($dbh, $pass_book_id);
}
##################### D E B U G ##############################################
print header,
start_html,
p("Track book data after final get<br>"),
p("book_ids: @book_ids<br>"),
p("pass_book_id: $pass_book_id - h_author_id: $h_author_id - h_last_name: $h_last_name<br>"),
p("m_id: $m_id - m_book_id: $m_book_id - m_isbn: $m_isbn - m_title: $m_title - m_author1_name: $m_author1_name - m_series_name: $m_series_name - m_series_num: $m_series_num - m_order_date: $m_order_date<br>"),
p("book_count: $book_count - c: $c - g: $g - c_count: $c_count - w_count: $w_count - s_count: $s_count<br>"),
p("-----------------------------------<br>"),
end_html;
exit;
##################### D E B U G ################################################################### D E B U G
exit;
}
&bail_out ("Invalid choice. - list_all.pl - Choice: $action");
&all_done();
exit;
sub get_html()
{
$page = `cat /home/rmelia/public_html/books/cgi_list.shtml`;
} # end sub get_html
sub put_html()
{
$page =~ s/<!--books: cgi\s*-->/$content/g;
print $html_header, $page;
} # end sub put_html
相关模块代码:
sub write_temp_tbl()
{
my $self = shift;
my $dbh = shift;
my $w_count = 0;
my $m_id = shift;
my $m_book_id = shift;
my $m_isbn = shift;
my $m_copyright_year = shift;
my $m_status = shift;
my $m_title = shift;
my $m_alt_title = shift;
my $m_notes = shift;
my $m_author_id = shift;
my $m_author1_name = shift;
my $m_author2_name = shift;
my $m_author_book_id = shift;
my $m_series_id = shift;
my $m_series_name = shift;
my $m_series_num = shift;
my $m_series_book_id = shift;
my $m_order_id = shift;
my $m_order_date = shift;
my $m_due_date = shift;
my $m_order_book_id = shift;
my $success = (1==2);
$sth = $dbh->prepare ("INSERT INTO webstone.temp
(m_id, m_book_id, m_isbn, m_copyright_year, m_status,
m_title, m_alt_title, m_notes,
m_author_id, m_author1_name, m_author2_name, m_author_book_id,
m_series_id, m_series_name, m_series_num, m_series_book_id,
m_order_id, m_order_date, m_due_date, m_order_book_id)
VALUES
('1', '$m_book_id', '$m_isbn', '$m_copyright_year', '$m_status',
'$m_title', '$m_alt_title', '$m_notes',
'$m_author_id', '$m_author1_name', '$m_author2_name', '$m_author_book_id',
'$m_series_id', '$m_series_name', '$m_series_num', '$m_series_book_id',
'$m_order_id', '$m_order_date', '$m_due_date', '$m_order_book_id')
");
$sth->execute();
if ( !$sth )
{
die "Couldn't execute insert statement: " . $dbh->errstr;
}
elsif ($sth->rows == 0)
{
die "INSERT INTO temp Failed - SQL error: " . $dbh->errstr . "<br>\n";
}
else
{
#die "SQL insert successful, rows = " . $sth->rows() . "<br>\n";
$success = (1==1);
}
$w_count++;
$sth->finish;
return ($success, $w_count);
} # end sub write_temp_tbl
我还尝试过用“for 循环”和“foreach 循环”来代替“while 循环”。我的研究都没有接近答案。