0

I have a custom self made registration form where a user needs to enter his address, I need to check if his address is already added in a custom table(which I have made manually with table prefix). I am using a custom function in my function.php file.

function my_custom_action(){ 
    $myinput = $_POST['address'];

    global $wpdb;
    $table_name = 'wp44_predefined_address';
    $result = $wpdb->get_row("SELECT * FROM wp44_predefined_address WHERE 'UPPER(Address1)' LIKE 'UPPER(%s)'", $myinput);
    print_r ($result); 
die(); 
}

All I am getting is a Null value in my console. I have converted the strings into Uppercase also, I have used quotes also. I have used LIKE, but I also used = , I am using $wpdb->get_row , I have also used , get_var or get_result. So what I am doing wrong ?

  • Do you have a dropdown of predefined addresses? If yes, you don't need LIKE, you only need =. If not, I'm afraid it's not that simple, as you'll need to build a fuzzy match library. – Ciprian Jul 19 '18 at 15:49
1

Replace this:

$result = $wpdb->get_row("SELECT * FROM wp44_predefined_address WHERE 'UPPER(Address1)' LIKE 'UPPER(%s)'", $myinput);

with this: (just choose the appropriate $like based on your requirements)

// Properly generate the LIKE query.
$like = '%' . $wpdb->esc_like( $myinput ) . '%'; // e.g. '%input%'
//$like = '%' . $wpdb->esc_like( $myinput ); // e.g. '%input'
//$like = $wpdb->esc_like( $myinput ) . '%'; // e.g. 'input%'

$result = $wpdb->get_row("SELECT * FROM wp44_predefined_address WHERE Address1 LIKE %s", $like);
  • 1
    I used this and it worked. global $wpdb; $result = $wpdb->get_results("SELECT * FROM wp44_predefined_address WHERE Address1 LIKE '%".$myinput."%' "); echo $wpdb->num_rows . 'Rows Found <br>'; print_r ($result); – user147190 Jul 19 '18 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy