• I’m having a problem with $wpdb->update (code below) throwing an error: unexpected T_STRING

    Any idea what might be wrong here?

    $wpdb->update('wp_network_members', $insert_member_data WHERE 'f_name' = $insert_member_data['f_name'] AND 'l_name' = $insert_member_data['l_name']);
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter jlknauff

    (@jlknauff)

    I realized I was missing a comma and don’t seem to need the single quotes on the field names, but I’m still getting the same error. Here is my updated line of code:

    $wpdb->update('wp_network_members', $insert_member_data, WHERE f_name = $insert_member_data['f_name'] AND l_name = $insert_member_data['l_name']);

    Try popping the clause in a variable first?

    $where = "WHERE f_name = {$insert_member_data['f_name']} AND l_name = {$insert_member_data['l_name']}'"
    $wpdb->update('wp_network_members', $insert_member_data, $where);

    Could do with seeing an example of what’s in the $insert_member_data array, obviously
    aside from f_name and l_name.

    That said, i think the update should look something like this..

    $wpdb->update(
        'wp_network_members', // Table
        $insert_member_data, // Array of key(col) => val(value to update to)
        array(
            'f_name' => $insert_member_data['f_name'],
            'l_name' => $insert_member_data['l_name']
        ) // Where
    );

    ..which should be inline with the example here.
    https://codex.www.ads-software.com/Function_Reference/wpdb_Class#Examples_7

    Without knowing more about what’s in $insert_member_data though it’s hard to say if the query needs further adjustment(it will do if any the columns you’re updating are numeric columns – or something not a string).

    You might find it easier to use $wpdb->query alongside $wpdb->prepare, so you can do your own UPDATE foo WHERE bar, etc.. without the fiddly formats you’d have to contend with using $wpdb->update.

    Thread Starter jlknauff

    (@jlknauff)

    I found the solution – I replaced the two items that the WHERE statement was using with an array of the two values and now it works fine.

    Example:

    $wpdb->update('wp_network_members', $insert_member_data, $insert_member_data_match);
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘$wpdb->update’ is closed to new replies.