$ wpdb-> insert_id不起作用

时间:2016-12-02 09:06:30

标签: mysql wordpress

我试图让最后一个插入id MySQL,但它不会返回任何内容。行已插入数据库中,$ wpdb-> last_query也会提供最后插入的查询。 这是我的代码

  global $wpdb;
  $table_name = $wpdb->prefix . "request_from";
  $wpdb->insert($table_name, 
      array('pre' => $prefix, 
          'first_name' => $first_name,  
          'middle_name' => $middle_initial, 
          'last_name' => $last_name, 
          'badge_name' => $name_badge, 
          'title' => $title, 
          'company' => $company, 
          'direct_mail' => $direct_mail, 
          'twitter' => $twitter_handle, 
          'direct_phone' => $direct_phone, 
          'address' => $address, 
          'address2' => $address_2,  
          'city' => $city, 
          'state' => $state, 
          'province' => $province, 
          'zip' => $zip_code, 
          'country' => $country, 
          'cc' => $cc, 
          'cc_contact' => $second_contact,  
          'cc_mail' => $second_email, 
          'cc_phone' => $second_phone)
        ); 
$x= $wpdb->last_query;
$id = $wpdb->insert_id 

我有一个名为id且具有自动增量值

的列
id  int(11)         No  None    AUTO_INCREMENT  

1 个答案:

答案 0 :(得分:0)

按照Codex中的wpdb参考进行故障排除:

  • 通过$ wpdb-> show_errors()
  • 启用数据库错误显示
  • 检查正在形成的查询并通过$ wpdb-> last_query
  • 运行

请通过打印或转储变量检查代码,

   global $wpdb;
      $table_name = $wpdb->prefix . "request_from";
      $wpdb->insert($table_name, 
          array('pre' => $prefix, 
              'first_name' => $first_name,  
              'middle_name' => $middle_initial, 
              'last_name' => $last_name, 
              'badge_name' => $name_badge, 
              'title' => $title, 
              'company' => $company, 
              'direct_mail' => $direct_mail, 
              'twitter' => $twitter_handle, 
              'direct_phone' => $direct_phone, 
              'address' => $address, 
              'address2' => $address_2,  
              'city' => $city, 
              'state' => $state, 
              'province' => $province, 
              'zip' => $zip_code, 
              'country' => $country, 
              'cc' => $cc, 
              'cc_contact' => $second_contact,  
              'cc_mail' => $second_email, 
              'cc_phone' => $second_phone)
            ); 
    $x= $wpdb->last_query;
    $id = $wpdb->insert_id;
// Let's output the last autogenerated ID.
  echo $wpdb->insert_id;

  // This returns the same result
  echo mysql_insert_id();