• I have created my own table on my wordpress database and created the filter search form, I also created result.php to my wordpress to output all searched products. Now, my problem is that, I dont know how to connect my database to my search filter form in wordpress.

    I already created some of it but its not running..

    Here’s my sample code

    <?php
    define('WP_USE_THEMES', false);
    global $wpdb;
    $destination = $_POST["destination"];
    
    echo "Welcome test" . $destination;
    
    $sql = "SELECT * FROM rates WHERE location = '" . $destination ."';";
    
    echo $sql;
    echo "search this";
    
    foreach ( $wpdb->get_results($sql) as $result ) {
     echo "search for". $result->location;
    }
    
    ?>

    https://goo.gl/photos/bqqAPiAkxF5dXCxY7

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Your code appears OK as far as it goes. There’s some improvements that need to be introduced eventually, but it should basically work if your query is correct. You can verify the query by running it directly in phpMyAdmin under the SQL tab.

    To catch many errors, define WP_DEBUG as true in wp-options.php.

    Where is your results.php placed? Theme folder? And it contains the above code? How is this page requested and/or where does your form submit to?

    FWIW, the improvements eventually needed include passing the input through stripslashes() to account for possible escaped apostrophes, quotes, etc. Validate and sanitize all form input. Use $wpdb->prepare() to construct your SQL query.

    Thread Starter mae48

    (@mae48)

    Hi bcworkz, thanks for your response…what do you mean by that??That code I wrote in there is a sample to call from phpmyadmin, that’s not actually the code to “search form submit” from my own created table in wp database.

    Where is your results.php placed? Theme folder? And it contains the above code? How is this page requested and/or where does your form submit to?

    I placed my result.php on my theme folder and yeah, it contains the code stated above to test if my result.php calls phpmyadmin. My form submits to results.php.

    here is my codes: https://drive.google.com/folderview?id=0B0Je1_NFqcfXNUdPMU1Udm9qRnM&usp=sharing

    Hope to hear from you soon..!

    Thread Starter mae48

    (@mae48)

    This is actually how I call it….I don’t know if I’m doing the right syntax.

    <?php

    require_once( $_SERVER[‘DOCUMENT_ROOT’] . ‘/wp-load.php’ );

    global $wpdb;

    $sql = “SELECT * FROM rates”;

    if(isset($_POST[‘destination’])){
    $search_term = mysql_real_escape_string($_POST[‘destination’]);
    $sql .=”WHERE location = ‘{search_term}'”;
    $sql .=” OR level ='{search_term}’ “;

    $query = mysql_query($sql) or die(mysql_error());
    }
    echo “your search term” . $query;
    ?>

    Moderator bcworkz

    (@bcworkz)

    Yeah, the require_once path is wrong. Without wp-load.php, nothing related to WP, like $wpdb will work. Directly requiring wp-load.php is considered a bad practice because it needs to be referenced relative to your code page and since the /wp-content/ folder can be moved in any WP installation, your code is not portable to other installations.

    If you don’t care about portability, then requiring wp-load.php will work, once you get the path correct that is. I believe the proper relative reference for default installations is ../../../wp-load.php, untested though, I may be off a level. I do not load WP this way.

    If you plan to develop more code for WP, I recommend getting in the habit of invoking the WP environment correctly. There’s several ways to do this, the following link is an article about the options available.
    https://glennmessersmith.com/pages/wpenviro.html

    Thread Starter mae48

    (@mae48)

    Thank you so much for your response bcworks…I have a solution to my problem..I used search all plugin to search the products I need. But I have several problems on my search form, I think I will search the solution on the internet or else I will go back here and ask question again…
    ??

    Thanks for your response anyway…

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Database not working’ is closed to new replies.