$sql = $wpdb->prepare("INSERT INTO {$wpdb->prefix}tbl_my_table
            ( 
              user_id,
              CreatedOn
            ) VALUES (
              %d,
              Now()
            )", $user_id);
  if (current_user_can('administrator'))
    $wpdb->show_errors();
  $wpdb->query($sql);

Get Auto ID Of New Record


  $Result = $wpdb->query($sql);
  if ($Result == True)
    return($wpdb->insert_id);
  else
    return(False);

The insert_id() function returns the auto generated id used in the latest querywith a column having the AUTO_INCREMENT attribute.

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published.