updating user in custom wordpress table

There are 2 main issues in your code:

  1. Your $wpdb->get_row($prepare) will return either an object or a null, so you can’t do if(count($users) > 0) because $users is not an array.

    To fix that, just use if ( $users ).

  2. There’s a typo here: SET fnmme=%s — the field name should be fname.

So you should just need to correct those issues and your code would work as expected.

However, you should not use $wpdb->get_results() to run an UPDATE query, and instead,

  • You can use $wpdb->query().

  • Or in your case, you should use $wpdb->update() like so: (just replace the $sql = $wpdb->prepare(...); $result = $wpdb->get_results($sql); in your code with the code below)

    $wpdb->update(
        'wp_registered_customers',
        array(
            'fname' => $fname,
            'email' => $email,
            'step1' => 1,
            'step2' => 0,
            'step3' => 0,
        ),
        array( 'email' => $email )
    );
    

    But, are you sure you want to set email to the value of $email? 🤔

Additionally, I don’t think it’s necessary to call esc_sql() here:

$fname = sanitize_text_field(esc_sql($_POST['fname']));
$email = sanitize_email(esc_sql($_POST['email']));

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)