47

我写如下

$name="Kumkum";
$email="kumkum@gmail.com";
$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);

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

4

8 回答 8

124

使用$wpdb->insert().

$wpdb->insert('wp_submitted_form', array(
    'name' => 'Kumkum',
    'email' => 'kumkum@gmail.com',
    'phone' => '3456734567', // ... and so on
));

来自@mastrianni 的补充:

$wpdb->insert为您清理数据,$wpdb->query这与需要您使用$wpdb->prepare. 两者之间的区别在于$wpdb->query允许您编写自己的 SQL 语句,其中$wpdb->insert接受一个数组并为您处理/sql 清理。

于 2013-08-07T06:58:21.873 回答
23

只需使用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 ,
));
?>
于 2013-08-07T07:26:30.543 回答
8

试试这个

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

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

$name     = "Kumkum"; //string value use: %s
$email    = "kumkum@gmail.com"; //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);

谢谢-弗兰克

于 2014-05-03T12:18:42.883 回答
6

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

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

因此,您最好清理价值观 - 始终考虑安全性。

于 2018-10-29T20:58:55.940 回答
5

你必须检查你的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);

或者你可以使用喜欢,

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

$wpdb->query($sql);

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

于 2013-08-07T06:55:41.157 回答
1

您的SQL中的问题:

你可以像这样构造你的sql:

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

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

$wpdb->insert('table_name', input_array())
于 2013-08-07T07:03:58.217 回答
1
global $wpdb;
$insert = $wpdb->query("INSERT INTO `front-post`(`id`, `content`) VALUES ('$id', '$content')");
于 2016-05-03T10:12:44.607 回答
-7
$wpdb->query("insert into ".$table_name." (name, email, country, country, course, message, datesent) values ('$name','$email', '$phone', '$country', '$course', '$message', )");
于 2014-12-01T11:32:28.740 回答