0

Im trying to create a plugin for a site where the user can see a table of orders from customers in the Wordpress dashboard, It's a custom db but I'm using the wp-list-table, I've wrecked my brain for hours trying to get the built in search for that working

I want to create a search form where the user can search this table and return the results in a nice little table for them.

This is my code so far for the search form

<form name="search" method="get"> 
    Order Number: <input type="text" name="find" />
    <input type="hidden" name="searching" value="yes" />
    <input type="submit" name="search" value="Search" /> 
</form>



<?php

//This is only displayed if they have submitted the form  
     if ($searching =="yes")  {  
        echo "<h2>Results</h2><p>";   
    //If they did not enter a search term we give them an error  
    if ($find == "")  {  
        echo "<p>Please Enter a search term";  
        exit;  }   

     // filtering 
     $find = strtoupper($find); 
     $find = strip_tags($find);  
     $find = trim ($find); 

     global $wpdb;

     //Now we search for our search term

     $database = $wpdb->get_row("SELECT * FROM storeuserdata2 WHERE orderno = '%$find%'");
     //And we display the results  
     echo "<table>";
        echo "<tr>";
            echo "<td>".$database->firstname."</td>";   
            echo "<td>".$database->lastname."</td>";
            echo "<td>".$database->email."</td>";
            echo "<td>".$database->address."</td>";
            echo "<td>".$database->towncity."</td>";
            echo "<td>".$database->contactnumber."</td>";
        echo "</tr>";
    echo "</table>";

    echo "<b>Searched For:</b> " .$find;  
 }  

This just hangs on a blank page and shows nothing atm. I want to display the results on the same page on the dashboard

I'm still a complete newb when it comes to php and Wordpress dev, so hopefully im just missing something simple

1
  • How did you go with this gidea?
    – Tim Malone
    Jul 6, 2016 at 3:49

1 Answer 1

1

Welcome to Wordpress development!

There's a few things to answer about your question, the first is regarding the hanging on a blank page. Usually that means there is an error of some sort; one of the most helpful things you'll find during development is turning on Wordpress' DEBUG mode, which will instruct PHP to print out errors when anything goes wrong. This will help you immensly in tracking down the issue.

To switch on debug mode, open wp-config.php in the root of your Wordpress install, look for WP_DEBUG, and set it to true. Make sure you set this back to false before your site goes live, because it's insecure to have errors going out to anyone but yourself.

Once you've done that, trying to use the search form again should give you a somewhat meaningful error and that'll help us track down where the issue is.

Now a couple of other things:

Unless PHP's register_globals setting is turned on (which it shouldn't be, because it's a security risk - and has actually been removed in more recent versions of PHP anyway), your code actually is not going to work because the variables you're calling haven't been set yet. To use the form data, you'll need to access the $_POST superglobal. Basically, each time you try to access a form field, do it like $_POST["searching"] instead of $searching. If you replace each of your field references with this, you might find that that solves the problem (unless there is something else I'm not noticing).

One other important thing to note is that your application is at risk of SQL injection. The user could enter anything in that search field... and if crafted carefully, it could do untold damage to your database. You'll want to read up on http://codex.wordpress.org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks and most likely use the $wpdb->prepare() function to protect your query.

I know that's probably a lot to take in; hopefully it guides you well, just let me know if you have any questions or if you get a different error after turning on DEBUG mode, and we can work through that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.