so I've tried creating a Wordpress plugin that searches data in a different MySQL database and it works when I search by license number but when I search for any kind of name I get sent to a "Page not found" page and I can't figure out how to fix it.
Here's the plugin:
<div class="search">
<form class="search-form" method="get" action="">
<input type="text" name="name" placeholder="Enter Name">
<input type="text" name="license_number" placeholder="Enter License Number">
<input type="submit" value="Search">
</form>
<?php
if (isset($_GET['name']) || isset($_GET['license_number'])) {
$name = sanitize_text_field($_GET['name']);
$license_number = sanitize_text_field($_GET['license_number']);
// Construct the search query
$conditions = [];
if (!empty($name)) {
$conditions[] = $second_db->prepare("name LIKE %s", '%' . $wpdb->esc_like($name) . '%');
}
if (!empty($license_number)) {
$conditions[] = $second_db->prepare("license_number LIKE %s", '%' . $wpdb->esc_like($license_number) . '%');
}
if (empty($conditions)) {
echo '<p>Please enter a search query.</p>';
return ob_get_clean();
}
$where_clause = implode(' OR ', $conditions);
// Debug: output the constructed query
error_log("Constructed query: SELECT * FROM licenses WHERE {$where_clause}");
// Query the second database for license results
$license_results = $second_db->get_results("SELECT * FROM licenses WHERE {$where_clause}");
// Debug: output the number of license results found
error_log("Number of license results: " . count($license_results));
// If searching by license number, use the name from the license result to search applications and courses
if (!empty($license_number) && !empty($license_results)) {
$name = $license_results[0]->name;
}
// Query the second database for application and course results using the name
$application_results = $second_db->get_results($second_db->prepare("SELECT * FROM applications WHERE name LIKE %s", '%' . $wpdb->esc_like($name) . '%'));
$course_results = $second_db->get_results($second_db->prepare("SELECT * FROM courses WHERE name LIKE %s", '%' . $wpdb->esc_like($name) . '%'));
// Debug: output the number of application and course results found
error_log("Number of application results: " . count($application_results));
error_log("Number of course results: " . count($course_results));
This is what the results should look like:
This is what I get when I search by any kind of name:
The field in the database is: "name" and the name format in the field is: "LastName, FirstName Middle Initial(if available)" or "Doe, John J".
For the URL with license results, it is: https://domain.tld/plugin/?name=&license_number=12520 Here are some examples of the URL when searching by name: https://domain.tld/plugin/?name=LAST+MICHAEL+R&license_number= OR https://domain.tld/plugin/?name=bob&license_number= OR OR https://domain.tld/plugin/?name=bob+lee&license_number=
My error log is inconsistent with updating so that hasn't been helpful.
Thank you for any help!
name
is a reserved term, you cant use it, try another variation, example userName
https://codex.wordpress.org/Reserved_Terms
this case same from this when adding term name
in url Wordpress 404 error when adding /?name=someonesname at the end of my URL