skip to Main Content

I use a WordPress site, I do my search on a custom database called "operations". The search is performed on the website.
I need to get other results from the table related to this row on request, not just what I entered. And get other data related to this string. Here is the search form on the site:

    <form method="post" action="https://site-name.com/wp-content/themes/theme/select_user.php">
    <label for="sku">SKU:</label><br/>
    <input type="text" name="sku" size="30"><br/>
    <label for="barcode">Barcode:</label><br/>
    <input type="text" name="barcode" size="30"><br/>
    <input id="submit" type="submit" value="Search"><br/>
    </form>
    </fieldset>

The database has the following columns: id, date, title, size, sku, barcode, price

File Contents select_user.php:


require( __DIR__ . '/../../../wp-load.php' );

global $wpdb;


$sku = trim($_REQUEST['sku']);
$barcode = trim($_REQUEST['barcode']);


$sql_select = $wpdb->get_results( 
    $wpdb->prepare( 
    "
    SELECT * FROM " . $wpdb->prefix . "operations 
    WHERE sku='$sku' || barcode='$barcode',
    ARRAY_N
    "
    )
);  


if ($sql_select)
{
    foreach($sql_select as $row)
    {
        echo 'SKU: ' . $row['sku'] .'</br>';
        echo 'Barcode: ' . $row['barcode'] .'</br>';
    }
        
}
else {
    echo 'No results';
    }

With this code, I get the answer "no results".
I would be grateful for any help

2

Answers


  1. Chosen as BEST ANSWER

    I created my own shortcode, and placed everything that is necessary there. Everything works.

    function custom_search_func( $atts ){
        
        echo '<fieldset>
        <form action="' . get_permalink() . '" method="POST">
        <label for="sku">SKU:</label><br/>
        <input type="text" name="sku" size="30"><br/>
        <label for="barcode">Barcode:</label><br/>
        <input type="text" name="barcode" size="30"><br/>
        <label for="date">Date:</label><br/>
        <input type="date" name="date" size="30"><br/>
        <input type="submit" value="Search">
        </form>
        </fieldset>';
    
        global $wpdb;
        $sku = trim($_REQUEST['sku']);
        $barcode = trim($_REQUEST['barcode']);
        $date = trim($_REQUEST['date']);
        
    
    $sql_select = $wpdb->get_results( 
        $wpdb->prepare( 
        "
        SELECT id, date, title, size, barcode, sku, price FROM " . $wpdb->prefix . "operations 
        WHERE sku='$sku' || barcode='$barcode' || date='$date'
        "
        )
    );  
    
    
    
    if ($sql_select)
    {
        echo '<table border="1" width="100%" cellpadding="5">';
        echo '<tr><th>ID</th>';
        echo '<th>Title</th>';
        echo '<th>Size</th>';
        echo '<th>SKU</th>';
        echo '<th>Barcode</th>';
        echo '<th>Price</th>';
        echo '<th>Date</th></tr>';
        foreach ( $sql_select as $id) {
            
            
        echo '<tr>';
            echo '<td>';
            echo $id->id;
            echo '</td>';
            echo '<td>';
            echo $id->title;
            echo '</td>';
            echo '<td>';
            echo $id->size;
            echo '</td>';
            echo '<td>';
            echo $id->sku;
            echo '</td>';
            echo '<td>';
            echo $id->barcode;
            echo '</td>';
            echo '<td>';
            echo $id->price;
            echo '</td>';
            echo '<td>';
            echo $id->date;
            echo '</td>';
            echo '</tr>';
            
        }
            echo '</table>';
            
    }
    else {
        echo 'No results';
        }
        
    }
     
    add_shortcode( 'customsearch', 'custom_search_func' );
    

    Use the [customsearch] shortcode to output this anywhere on the page


  2. use the post hook for wp forms not directly use .php file

    add_action( 'admin_post_add_foobar', 'prefix_admin_add_foobar' );
    function prefix_admin_add_foobar() {
        // Handle request then generate response using echo or leaving PHP and using HTML
    }
    

    for your form use :

    <form action="http://www.example.com/wp-admin/admin-post.php" method="post">
    <input type="hidden" name="action" value="add_foobar">
    <input type="hidden" name="data" value="foobarid">
    <input type="submit" value="Submit">
    </form>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search