php - SQL Query Search page

admin2025-01-07  5

I am trying to create a Wordpress webpage that has an input text box and a button. what I want to do is take the input text from the text box and use it as a parameter to search a table I put into the website database and print the results on the screen. I tried using PHP to no avail. what is the best to do this? here is the code

Search the Film Database <input id="input" name="input" type="text" /> [php] global $wpdb; echo '<button type="button" onclick="myFunction()">Search</button>'; $text = sanitize_text_field($_POST['input']); echo $text; $wpdb->query('USE vcuw2938212664'); function myFunction(){ $txt = sanitize_text_field($_POST['input']); echo 'text is "$txt; $query = 'SELECT * FROM documentaries WHERE English_Title = \'%Li%\';'; echo $query; $results = $wpdb->get_results($query); if ($results != Null){ echo "{$results}"; } else { echo 'no results returned'; } } [/php] 

I am trying to create a Wordpress webpage that has an input text box and a button. what I want to do is take the input text from the text box and use it as a parameter to search a table I put into the website database and print the results on the screen. I tried using PHP to no avail. what is the best to do this? here is the code

Search the Film Database <input id="input" name="input" type="text" /> [php] global $wpdb; echo '<button type="button" onclick="myFunction()">Search</button>'; $text = sanitize_text_field($_POST['input']); echo $text; $wpdb->query('USE vcuw2938212664'); function myFunction(){ $txt = sanitize_text_field($_POST['input']); echo 'text is "$txt; $query = 'SELECT * FROM documentaries WHERE English_Title = \'%Li%\';'; echo $query; $results = $wpdb->get_results($query); if ($results != Null){ echo "{$results}"; } else { echo 'no results returned'; } } [/php] 
Share Improve this question edited Mar 5, 2021 at 22:09 Tom J Nowell 60.7k7 gold badges77 silver badges147 bronze badges asked Oct 27, 2016 at 22:14 user3533573user3533573 13 bronze badges 5
  • 1 Can you show what you have tried in your question please? – Nathan Powell Commented Oct 27, 2016 at 22:58
  • sure! here is my code! – user3533573 Commented Nov 4, 2016 at 13:56
  • Search the Film Database <input id="input" name="input" type="text" /> [php] global $wpdb; echo '<button type="button" onclick="myFunction()">Search</button>'; $text = sanitize_text_field($_POST['input']); echo $text; $wpdb->query('USE vcuw2938212664'); function myFunction(){ $txt = sanitize_text_field($_POST['input']); echo 'text is "$txt; $query = 'SELECT * FROM documentaries WHERE English_Title = \'%Li%\';'; echo $query; $results = $wpdb->get_results($query); if ($results != Null){ echo "{$results}"; } else { echo 'no results returned'; } } [/php] – user3533573 Commented Nov 4, 2016 at 13:57
  • Please put the code in your question and format it. – Nathan Powell Commented Nov 4, 2016 at 14:17
  • 1 You shouldn't be using plugins that embed PHP code in [php] shortcodes, they're a major security risk – Tom J Nowell Commented Mar 5, 2021 at 22:10
Add a comment  | 

1 Answer 1

Reset to default 0

you can use WP_Query(); to query the results.

it has an argument 's' => $search_query,

create an input, get the input value, store it in $search_query, then create a query,

$query = new WP_Query( array( 'post_type' => 'post', 's' => $search_query ) );
if ( $query->have_posts() ) : 
     while ( $query->have_posts() ) : $query->the_post();

     // do something...

     endwhile;
     wp_reset_postdata();
endif;
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736256378a355.html

最新回复(0)