[ic] perl db question, problems when modifying admin

Mike Heins interchange-users@interchange.redhat.com
Thu Feb 14 19:17:00 2002


Quoting edmz (edmz@yahoo.com):
> I am adding another page to the admin, for new products. Since my
> store doesnt carry all the products from the distributor, I have
> created a table that holds all the new products. Someone
> will manually check the list and only import certain products
> to the database.
> 
> Searching the admin pages, I found that customer.html was very
> close to what I needed, so I modified a copy of it. Now
> I can browse all the new products and remove the unwanted but
> I am having problem importing them.
> 
> I dont know how to insert a new record in the DB nor how
> to get the item data to fill the query.
> 
> My approach was something like:
> 
> [perl tables="new_products products"]
>          my $db = $Db{new_products};
>          ...
>          ...
>          ...
>         if($CGI->{imporitem}){
>            my $dbtemp = $DB{products};
                            ^
	That's a typo, but  you have it correct above ($Db).

>            for(grep $_, @{$CGI_array->{itemsku}}) {
>               $dbtemp->set_field($_, "sku", $_);
>               $dbtemp->set_field($_, "description", $db->field($_,
> "description") );

If the tables are identical, easier and faster than above would be:

             for(grep $_, @{$CGI_array->{itemsku}}) {
		 my @fields = $db->row($_);
		 next unless @fields;
		 $dbtemp->set_row(@fields);
	     }

If only some fields are to be inserted:

	     my @cols = qw/sku description price comment etc/;
             for(grep $_, @{$CGI_array->{itemsku}}) {
		my $row = $db->row_hash($_);
		my @vals = @{$row}{@cols};
		$dbtemp->set_slice($_, [ @cols ], [ @vals ]);	
	     }

-- 
Red Hat, Inc., 3005 Nichols Rd., Hamilton, OH  45013
phone +1.513.523.7621      <mheins@redhat.com>

Friends don't let friends use Outlook. -- Bob Blaylock