0

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

1
  • I have checked it with $wpdb->get_results() and the query $wpdb->get_results("SELECT auth_code FROM $table_name WHERE auht_code = '$prod_auth'"); as well but not working. Commented Nov 14, 2018 at 6:45

1 Answer 1

1

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 );
Sign up to request clarification or add additional context in comments.

1 Comment

Perhaps the value for $prod_auth used in the where clause doesn't exist in the table. You can check to confirm if there is an error running the query wpdb->print_error()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.