php 如何使用wpdb插入数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18096555/
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-25 16:58:29  来源:igfitidea点击:

How to insert data using wpdb

phpwordpresswpdb

提问by Nida

I have wriiten as following

我写如下

$name="Kumkum";
$email="[email protected]";
$phone="3456734567";
$country="India";
$course="Database";
$message="hello i want to read db";
$now = new DateTime();
$datesent=$now->format('Y-m-d H:i:s');    
global $wpdb;
$sql = $wpdb->prepare(
 "INSERT INTO `wp_submitted_form`      (`name`,`email`,`phone`,`country`,`course`,`message`,`datesent`) values ("
 $name, $email, $phone, $country, $course, $message, $datesent. ')")';

$wpdb->query($sql);

It's not working... It throws error... Please help me in correcting it.

它不起作用......它抛出错误......请帮助我纠正它。

回答by Nadh

Use $wpdb->insert().

使用$wpdb->insert().

$wpdb->insert('wp_submitted_form', array(
    'name' => 'Kumkum',
    'email' => '[email protected]',
    'phone' => '3456734567', // ... and so on
));

回答by Yudha Setiawan

Just use wpdb->insert(tablename, coloumn, format)and wp will prepare that's query

只需使用wpdb->insert(tablename, coloumn, format)wp 将准备该查询

<?php
global $wpdb;
$wpdb->insert("wp_submitted_form", array(
   "name" => $name,
   "email" => $email,
   "phone" => $phone,
   "country" => $country,
   "course" => $course,
   "message" => $message,
   "datesent" => $now ,
));
?>

回答by Frank

Try this

尝试这个

I recently leaned about $wpdb->prepareHEREand added into our Free Class Booking plugin, plugin approved on wordpress.org and will live soon:

我最近倾向于$wpdb->prepareHERE并添加到我们的免费课程预订插件中,该插件已在 wordpress.org 上获得批准,并将很快上线:

global $wpdb;
$tablename = $wpdb->prefix . "submitted_form";

$name     = "Kumkum"; //string value use: %s
$email    = "[email protected]"; //string value use: %s
$phone    = "3456734567"; //numeric value use: %d
$country  = "India"; //string value use: %s
$course   = "Database"; //string value use: %s
$message  = "hello i want to read db"; //string value use: %s
$now      = new DateTime(); //string value use: %s
$datesent = $now->format('Y-m-d H:i:s'); //string value use: %s

$sql = $wpdb->prepare("INSERT INTO `$tablename` (`name`, `email`, `phone`, `country`, `course`, `message`, `datesent`) values (%s, %s, %d, %s, %s, %s, %s)", $name, $email, $phone, $country, $course, $message, $datesent);

$wpdb->query($sql);

Thanks -Frank

谢谢-弗兰克

回答by Rohan Kumar

You have to check your quotesproperly,

你必须quotes正确检查你的,

$sql = $wpdb->prepare(
    "INSERT INTO `wp_submitted_form`      
       (`name`,`email`,`phone`,`country`,`course`,`message`,`datesent`) 
 values ($name, $email, $phone, $country, $course, $message, $datesent)");
$wpdb->query($sql);

ORyou can use like,

或者你可以使用像,

$sql = "INSERT INTO `wp_submitted_form`
          (`name`,`email`,`phone`,`country`,`course`,`message`,`datesent`) 
   values ($name, $email, $phone, $country, $course, $message, $datesent)";

$wpdb->query($sql);

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

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

回答by T.Todua

The recommended way (as noted in codex):

推荐的方式(如codex 中所述):

$wpdb->insert( $table_name, array('column_name_1'=>'hello', 'other'=> 123), array( '%s', '%d' ) );

So, you'd better to sanitize values - ALWAYS CONSIDER THE SECURITY.

因此,您最好对值进行消毒 - 始终考虑安全性。

回答by som

Problem in your SQL:

您的SQL 中的问题:

You can construct your sql like this :

您可以像这样构建您的 sql:

$wpdb->prepare(
 "INSERT INTO `wp_submitted_form` 
   (`name`,`email`,`phone`,`country`,`course`,`message`,`datesent`) 
   values ('$name', '$email', '$phone', '$country', 
         '$course', '$message', '$datesent')"
 );

You can also use $wpdb->insert()

你也可以使用 $wpdb->insert()

$wpdb->insert('table_name', input_array())

回答by paras

global $wpdb;
$insert = $wpdb->query("INSERT INTO `front-post`(`id`, `content`) VALUES ('$id', '$content')");

回答by Jitender Bhargava

$wpdb->query("insert into ".$table_name." (name, email, country, country, course, message, datesent) values ('$name','$email', '$phone', '$country', '$course', '$message', )");