MySQL 如何在wordpress数据库中插入查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21964899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:06:28  来源:igfitidea点击:

How to insert query in wordpress database

mysqlwordpress

提问by Alyssa Reyes

I have a email subscription to my site and I want to insert to my Wordpress database so I can export the email listing. I already created table wp_email_subscription with 4 fields ID, name, email and date created. what will be the query for this? is there any wordpress database script to use?

我有一个电子邮件订阅到我的网站,我想插入到我的 Wordpress 数据库中,以便我可以导出电子邮件列表。我已经创建了表 wp_email_subscription,其中包含 4 个字段 ID、姓名、电子邮件和创建日期。对此的查询是什么?有什么wordpress数据库脚本可以使用吗?

回答by Brett DeWoody

Wordpress provides the $wpdbclass of functions for interacting with the database.

Wordpress 提供了$wpdb与数据库交互的函数类。

To insert an email address you could do something like:

要插入电子邮件地址,您可以执行以下操作:

<?php 

  $wpdb->insert('wp_email_subscription', 
    array(
      'name'          => 'name',
      'address'       => '[email protected]'
    ),
    array(
      '%s',
      '%s'
    ) 
  ); 

?> 

More info on the Wordpress Codex.

关于Wordpress Codex 的更多信息。

回答by wingskush

$wpdb->query("INSERT INTO wp_email_subscription (name, email, date) VALUES ('$name', '$email', '$date')"  );

This is if you want to insert values to your table. You do not have to use $wpdb->email_subscription for the prefix as it's the table you created yourself, otherwise, if you were inserting values to default WordPress tables you would prefer doing $wpdb->users etc.

这是如果您想向表中插入值。您不必使用 $wpdb->e​​mail_subscription 作为前缀,因为它是您自己创建的表,否则,如果您将值插入到默认的 WordPress 表中,您会更喜欢使用 $wpdb->users 等。

回答by Pranay Bhardwaj

you can go for

你可以去

global $wpdb;
$wpdb->insert('wp_email_subscription',array('name'=>$name,'email'=>$email),array('%s','%s'));

go through this for better understanding:

通过这个更好地理解:

http://codex.wordpress.org/Class_Reference/wpdb

http://codex.wordpress.org/Class_Reference/wpdb

回答by rajan snuriya

 global $wpdb

 $wpdb->insert('wp', array(
                        'email' => $_POST['email'],
                        'city'  =>   $_POST['city'],
                        'state' =>$_POST['state'],
                        'phone' => $_POST['phone'],
                       'mobile' => $_POST['mobile'],
                       )
             );

回答by ucoder92

function insert($array = false)
{
    global $wpdb;
    return $wpdb->insert($wpdb->prefix . 'email_subscription', $array);
}