I have a strange error here. im trying to write to the wordpress db but $wpdb->insert does not write the variable first name.

Can any one spot the error in this code?

function pay_record_register( $first_name = "NA", 
                            $last_name = "NA", 
                            $payer_email = "NA", 
                            $item_name = "NA", 
                            $gross = "NA", 
                            $currency = "NA", 
                            $payment_status = "NA", 
                            $payment_date = "NA", 
                            $address_street = "NA", 
                            $address_zip = "NA", 
                            $address_city = "NA", 
                            $address_country = "NA", 
                            $address_status = "NA"  ) {

global $wpdb;

$table_name = $wpdb->prefix . 'pay_record';

$wpdb->insert($table_name, array( 'last_name' => $last_name, 
                                    'first_name' => $first_name,
                                    'payer_email' => $payer_email, 
                                    'item_name' => $item_name, 
                                    'gross' => $gross, 
                                    'currency' => $currency, 
                                    'payment_status' => $payment_status, 
                                    'payment_date' => $payment_date, 
                                    'address_street' => $address_street, 
                                    'address_zip' => $address_zip, 
                                    'address_city' => $address_city, 
                                    'address_country' => $address_country, 
                                    'address_status' => $address_status


In my DB every variable is printed with the default (i dident send any values) except first_name. first_name is left empty...

Here is a picture of my table structure: enter image description here

I'm great full for any tips, tricks or answers :)

1 Answer 1


I don't see anything wrong with the code as far as $first_name is concerned. My guess is that your actual DB column name is spelled incorrectly in the schema, or is otherwise improperly set up (maybe it's the wrong type, length, etc.).

This doesn't answer your question, but I do have a tip: when a function has this many params (especially when so many are optional), it's cleaner to pass them as an array. This is how WordPress itself handles functions with lots of params, and there are some helper functions to make it easy. An example function in this style might look like:

wpsx_53999_parse_args_example($arr_args) {

    // Deafults array
    $arr_defaults = array(
        last_name => 'NA', 
        payer_email => 'NA', 
        item_name => 'NA', 
        gross => 'NA', 
        currency => 'NA', 
        payment_status => 'NA', 
        payment_date => 'NA', 
        address_street => 'NA', 
        address_zip => 'NA', 
        address_city => 'NA', 
        address_country => 'NA', 
        address_status => 'NA',
    extract( wp_parse_args($arr_args, $arr_defaults), EXTR_SKIP );

    // Now, all the params are extracted as in-scope variables!
    // You can use them like, $first_name, $last_name, etc. from here on out


In this example, the wp_parse_args() function compares any passed values against the defaults, and essentially reconciles the two. The extract() function pulls all the bits of the final array into local scope.

The biggest advantage here is that when you're actually calling your function, you only need to pass in an array of necessary values. So you don't have to fill up the call with "FALSE" or "NULL" placeholder values just to keep things in the right order.

  • hmmm, i have checked all the db table names, can't find any typos. but thank you for the default array :) Jun 1, 2012 at 21:48
  • @OleHenrikSkogstrøm Not table names--column names. Can you post a dump of your schema for the "pay_record" table?
    – MathSmath
    Jun 2, 2012 at 2:16
  • Oh, i think i just wrote table instead of column. I have added a picture of my table structure. Jun 2, 2012 at 13:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.