从Wordpress自定义表中搜索数据

时间:2016-01-18 07:39:07

标签: php wordpress wpdb

我正在开发一个许可证管理器插件,但无法从我的wordpress自定义表中搜索数据。请帮忙!!

表格ITEM为licence_id, product_id, licence_code, licence_status, activation_date, creation_date,

<form method="post">
<label><?php _e('Input License here : '); ?></label>
<input type="text" name="codename" />
<button type="submit" name="submit" class="submit button"><?php _e('Verify Now'); ?></button><br> 
</form>


        <?php
        if (isset($_POST['submit'])){
            $code_qr = $_POST["codename"];

            global $wpdb;
            $query = "SELECT licence_code FROM {$wpdb->prefix}wc_product_licences WHERE licence_code = '$code_qr'";   
            $lic_codes = $wpdb->get_var($query);

            if ($lic_codes == $code_qr) {
              echo  "<p>Your License : ".$lic_codes. " is Valid</p>";
            }else{
                echo"Sorry!! No License match with your query";
            }
        }
    ?>

1 个答案:

答案 0 :(得分:0)

解决!!!

<?php
    if (isset($_POST['submit'])){

        $code_qr = $_POST["codename"];
        global $wpdb;
        $query = "SELECT * FROM {$wpdb->prefix}wc_product_licences WHERE licence_code = '$code_qr'";    
        $lic_codes = $wpdb->get_results($query);
        foreach ($lic_codes as $mycode){
            $final_code = $mycode->licence_code;
        }

        //Generate Message .
        if ($final_code ) {
                echo "<p>Your License : <code>".$final_code. "</code> is Valid</p>";
            }else{
                echo"Sorry!! No License match with your query";
            }
    }