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!
2
Answers
i don’t know why your plugin not work, but you can try to change get param in your url with something else, maybe
userName
, orperson
, orfullName
. then change your query with double quote in your fieldname
, i worry about word "name" make error in your sql query.I think it’s because of missed quotes of text value. Try to change your condition like this
$conditions[] = $second_db->prepare("name LIKE '%s' ", '%' . $wpdb->esc_like($name) . '%');
Also, you could dump the raw query string and run it in the database console directly to see the output.
echo $wpdb->last_query; die;
Also, if you are on a development server try to switch on the error logs, I guess you have an exception redirect in your code routine.