最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Search database with WPDB using a php variable

programmeradmin2浏览0评论

I'm trying to do a basic search feature for my website database. I want to search the text content inside a custom database table using Wordpress and WPDB.

For some reason the search is not returning anything from the database but I'm also not getting any error messages. Here is my code:

thePage.php:

<form class="searchCV_form" role="form" action="">
     <div>
         <input type="text" id="search_text" name="search_text" class="cv__text">
         <span class="input-group-btn">
             <button type="submit" class="btn btn-default btn-primary cv__button search--form-btn">SUBMIT</button>
         </span>
     </div>
 </form>
 <div id="search_results"></div>

 <script>
// wrap everything in a closure
(function($){

  // get our references
  var $form = $('form.searchCV_form'),
      $search_field = $('#search_text'),
      $results = $('#search_results');

  // AJAX search call
  function do_search() {

    // grab the query value from the search field
    var search_text = $search_field.val();

    // do a POST ajax call
    $.ajax({
      type: "POST",
      url: '<?php echo admin_url('admin-ajax.php'); ?>',
      data: ({
        action: "search_cv",
        search_text: search_text
      }),
      success: function (response){
        console.log(response);
        $results.html(response);
      }
    });
  }

  // on submit, do the search but return false to stop page refresh
  $form.submit(function(e) {
    do_search();
    return false;
  });

})(jQuery);
</script>

Functions.php:

function search_cv()
{
    // get the search query
    $search_text = ($_POST["search_text"]);

    // clean it up
    $search_text = sanitize_text_field( $search_text);

    // ... do stuff with it
    global $wpdb;

    $result = $wpdb->get_results( $wpdb->prepare( "SELECT b, c, v, t FROM myTableName WHERE t like %s", $search_text), ARRAY_A ); 

    //output the HTML which will be consumed by $.html()

    ?><div>You searched for <?php echo $search_text; ?> and we found... <?php 

    foreach ($result as $row) {
    echo $row->t; 
    }

    ?></div><?php

    // stop doing stuff
    die();
}

add_action( 'wp_ajax_search_cv', 'search_cv' ); 
add_action( 'wp_ajax_nopriv_search_cv', 'search_cv' );

I suspect that the mistake is in my use of $wpdp->prepare or the SQL query, but I have no idea what could be wrong there.

I'm trying to do a basic search feature for my website database. I want to search the text content inside a custom database table using Wordpress and WPDB.

For some reason the search is not returning anything from the database but I'm also not getting any error messages. Here is my code:

thePage.php:

<form class="searchCV_form" role="form" action="">
     <div>
         <input type="text" id="search_text" name="search_text" class="cv__text">
         <span class="input-group-btn">
             <button type="submit" class="btn btn-default btn-primary cv__button search--form-btn">SUBMIT</button>
         </span>
     </div>
 </form>
 <div id="search_results"></div>

 <script>
// wrap everything in a closure
(function($){

  // get our references
  var $form = $('form.searchCV_form'),
      $search_field = $('#search_text'),
      $results = $('#search_results');

  // AJAX search call
  function do_search() {

    // grab the query value from the search field
    var search_text = $search_field.val();

    // do a POST ajax call
    $.ajax({
      type: "POST",
      url: '<?php echo admin_url('admin-ajax.php'); ?>',
      data: ({
        action: "search_cv",
        search_text: search_text
      }),
      success: function (response){
        console.log(response);
        $results.html(response);
      }
    });
  }

  // on submit, do the search but return false to stop page refresh
  $form.submit(function(e) {
    do_search();
    return false;
  });

})(jQuery);
</script>

Functions.php:

function search_cv()
{
    // get the search query
    $search_text = ($_POST["search_text"]);

    // clean it up
    $search_text = sanitize_text_field( $search_text);

    // ... do stuff with it
    global $wpdb;

    $result = $wpdb->get_results( $wpdb->prepare( "SELECT b, c, v, t FROM myTableName WHERE t like %s", $search_text), ARRAY_A ); 

    //output the HTML which will be consumed by $.html()

    ?><div>You searched for <?php echo $search_text; ?> and we found... <?php 

    foreach ($result as $row) {
    echo $row->t; 
    }

    ?></div><?php

    // stop doing stuff
    die();
}

add_action( 'wp_ajax_search_cv', 'search_cv' ); 
add_action( 'wp_ajax_nopriv_search_cv', 'search_cv' );

I suspect that the mistake is in my use of $wpdp->prepare or the SQL query, but I have no idea what could be wrong there.

Share Improve this question asked Apr 13, 2019 at 20:05 AnttiAntti 1272 silver badges7 bronze badges 3
  • 2 First off, you put array_a but then you use object notation for the output $row->t – Howard E Commented Apr 13, 2019 at 21:52
  • 1 It looks like you are using where t like "search_text" instead of where t like "%search_text%" , i.e. you will only get results if the entire t field is equal to the search text, not if the t field only contains the search text. – Jos Commented Apr 14, 2019 at 13:22
  • Thank you, both comments helped and it works now! – Antti Commented Apr 14, 2019 at 20:31
Add a comment  | 

1 Answer 1

Reset to default 1

This code in the functions.php file made it work (based on the two helpful comments):

function search_cv()
{
    // get the search query
    $search_text = ($_POST["search_text"]);

    // clean it up
    $search_text = sanitize_text_field( $search_text);

    // ... do stuff with it
    global $wpdb;

    $result = $wpdb->get_results( $wpdb->prepare( "SELECT b, c, v, t FROM myTableName WHERE t like %s", "%$search_text%"), ARRAY_A ); 

    //output the HTML which will be consumed by $.html()

    ?><div>You searched for <?php echo $search_text; ?> and we found... <?php 

    foreach ($result as $row) {
    echo $row[t]; 
    }

    ?></div><?php

    // stop doing stuff
    die();
}

add_action( 'wp_ajax_search_cv', 'search_cv' ); 
add_action( 'wp_ajax_nopriv_search_cv', 'search_cv' );
发布评论

评论列表(0)

  1. 暂无评论