I am working on a MySQL database that is accessible via browser using the Catalyst framework. The user can search the database from a search box using a specific ID (car plate) and/or a specific car color.
The problem is that when I search for some IDs that exist in the database they don't show up in the results whereas others do. All of the IDs that I test can be accessed properly if I manually change the URL in the browser, so I guess the problem lies in my search function.
My function in the controller is this
sub search : Local : Args(0) {
my ($self, $c) = @_;
my $schema = $c->model('DB');
my $car_plate = $c->request->params->{car_plate} || 'N/A';
my $car_model = $c->request->params->{car_model} || 'N/A';
my $result_set = "";
my $result_set_count = 0;
if (($car_plate ne 'N/A') && ($car_model ne 'Select model')) { # car plate AND car model entered
$result_set = [
$schema->resultset('Car')->search({
'plates' => { 'like', '%' . $car_plate . '%' },
'colors' => $car_model
},
)->all
];
}
elsif (($car_plate eq 'N/A') && ($car_model ne 'Select model')) { # car plate blank, car model entered
$result_set = [
$schema->resultset('Car')->search(
{ 'colors' => $car_model },
)->all
];
}
elsif (($car_plate ne 'N/A') && ($car_model eq 'Select model')) { # car plate entered, car model blank
$result_set = [
$schema->resultset('Car')->search(
{ 'plates' => { 'like', '%' . $car_plate . '%' } },
)->all
];
}
if (($car_plate ne 'N/A') && ($car_model ne 'Select model')) {
$result_set_count = $c->model('DB::Car')->search({
'plates' => { 'like', '%' . $car_plate . '%' },
'colors' => $car_model
}
)->all;
}
elsif (($car_plate eq 'N/A') && ($car_model ne 'Select model')) {
$result_set_count = $c->model('DB::Car')->search({ 'colors' => $car_model })->all;
}
elsif (($car_plate ne 'N/A') && ($car_model eq 'Select model')) {
$result_set_count = $c->model('DB::Car')->search({ 'plates' => { 'like', '%' . $car_plate . '%' } })->all;
}
my $error_message = "";
if ($result_set_count == 0) {
$error_message = "<p>Not found</p>";
$c->stash(
errorcount => $error_message,
Cars => $result_set,
template => 'cars/search_no_results.tt'
);
}
else {
$c->stash(
errorcount => $error_message,
Cars => $result_set,
template => 'cars/search_cars.tt'
);
}
$c->response->header('Cache-Control' => 'no-cache');
}