Shahid Khattak
Shahid Khattak

Reputation: 69

How to compare User Input to Custom WordPress Database Table

I have created a plugin which creates a table in the database, I have inserted some values in that table as well. The table has only two columns which are ID and auth_code, the ID column is the primary key and autoincrement, so the only column to be considered is the auth_code column. The table creation and data insertion are working perfectly. I have added a shortcode which I want to use for the user to input a code (on the front end) in a single field and my plugin will compare it with the auth_code field in the custom table and return TRUE or False. The auth_code column is having values which are all unique, so there should be only one match or no match. My shortcode code is here

<?php

add_shortcode( 'prod_auth', 'product_auth_shortcode' );

function product_auth_shortcode(){

global $wpdb;

$table_name = $wpdb->prefix.'wpa_product_auth';

if(isset($_POST['submit'])){

    $prod_auth = $_POST['auth_code'];

    $check = $wpdb->get_col("SELECT * FROM  $table_name WHERE auht_code = '$prod_auth'");



    if($check->num_rows == 1){
        echo "<script>alert('Auth')</script>";
    }else{
        echo "<script>alert('Not Auth')</script>";
    }

}

?>
<form class="form-inline" method="post">
  <div class="form-group mb-2">

    <input type="text" readonly class="form-control-plaintext" value="Please 
Enter Your Unique Code">
  </div>
  <div class="form-group mx-sm-3 mb-2">
    <label for="UniqueCode" class="sr-only">Enter Your Unique Code</label>
    <input type="text" class="form-control" id="inputPassword2" placeholder="" name="auth_code">
  </div>

  <button type="submit" class="btn btn-primary mb-2">Authenticate</button>
</form>
<?php

}

I have added this shortcode to one of my pages, the form is showing up there, but not returning any results. Any Help in this regard will be appreciated. Thanks

Upvotes: 0

Views: 1078

Answers (1)

Oluwafemi Sule
Oluwafemi Sule

Reputation: 38962

There is a typo in the name for the column in filter. It should be auth_code and not auht_code;

$prepared_statement = $wpdb->prepare(
    "SELECT auth_code FROM {$table_name} WHERE auth_code = %s", $prod_auth );

$values = $wpdb->get_col( $prepared_statement );

Upvotes: 1

Related Questions