Check if Value Exists in Database, adding row details to variables and echoing result


I am trying to create a UK Postcode checker for a clients Woocommerce shop. I have created a table in WordPress with the cells, Postcode, Town and County.

When a user enters their postcode into a form and clicks submit, I want to check if the postcode exists in our database, and let the user know.

My code is not recognising whether the postcode exists in the database or outputting the town later.

Any help would be most gratefully appreciated.

global $wpdb;
$table_name = $wpdb->prefix.'vw_postcode_checker';

if (!empty($_POST[postcode]))
    $postcode = $_POST[postcode];
    $entry = $wpdb->get_results("SELECT * FROM ".$tablename."WHERE postcode =" . $postcode);
    if(count($entry) == 1){
         $id = $entry->id;   //We need this for the delete to work.
         $postcode = $entry->postcode;
         $town = $entry->town;
         $county = $entry->county;
         $confirmed = "Contratulations, we are delivering to" . $town;
         echo $confirmed;
    else {
    $notconfirmed = "Sorry, we are not delivering to your area at this time.";
    echo $notconfirmed;

Victoria Whitehead 3 years 2020-05-30T04:11:07-05:00 0 Answers 82 views 0

Leave an answer