PDA

View Full Version : insert of data puts records the wrong way around


bazz
11-04-2008, 06:31 PM
Hi,

:confused::confused:
The following sub routine puts the data I need into the db but, the records are not in the ascending order of the PK. The PK autoincremental id could be 345, 347,346.

1. Does this matter?
2. why is it doing it?
3. how would I fix it if I need to?


sub input_room_option_costs {

my $booking_id = $session->param('booking_id');
my %options = % {$session->param('options_data') };

foreach my $chosen_option (sort keys %options)
{
#print qq( chosen_option = $chosen_option<br />
#cost = $options{$chosen_option}
#);

my $insert_options_costs = $bookings_db_connect->prepare("INSERT INTO
tbl_room_bills
(
room_bill_id,
booking_id,
date,
quantity,
description,
cost
)
VALUES ( 'DEFAULT',
'$booking_id',
'',
'',
'$chosen_option',
'$options{$chosen_option}'
)
") or die "prepare statement failed: $DBI::errstr\n";
$insert_options_costs->execute;
}

}




bazz

KevinADC
11-04-2008, 07:54 PM
the default sort is ascii not numeric. If you want numeric sort use the longer version of sort and the <=> operatopr:

sort {$a <=> $b} keys %hashname

if you need to sort the values instead of the keys:

sort {$hash{$a} <=> $hash{$b}} keys %hashname

bazz
11-04-2008, 07:59 PM
Thanks Kevin,

I didn't think it was the hash getting jumbled but the MySQL. The order of the rows doesn't matter to me but I thought that the ID col, should be in order since it is set to autoincrement.

I'll change my code and see how I get on with it.

:thumbsup:

bazz

KevinADC
11-04-2008, 08:01 PM
ahh...I may have misunderstood.