我写的如下
$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);
它不起作用......它会引发错误......请帮助我纠正它。
答案 0 :(得分:76)
使用$wpdb->insert()
。
$wpdb->insert('wp_submitted_form', array(
'name' => 'Kumkum',
'email' => 'kumkum@gmail.com',
'phone' => '3456734567', // ... and so on
));
答案 1 :(得分:16)
只需使用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 ,
));
?>
答案 2 :(得分:4)
您必须正确检查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);
或你可以使用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);
答案 3 :(得分:3)
试试这个
我最近倾向于关注$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);
由于 -Frank
答案 4 :(得分: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())
答案 5 :(得分:1)
$wpdb->insert( $table_name, array('column_name_1'=>'hello', 'other'=> 123), array( '%s', '%d' ) );
因此,您最好清理价值-始终考虑安全性。
答案 6 :(得分:0)
global $wpdb;
$insert = $wpdb->query("INSERT INTO `front-post`(`id`, `content`) VALUES ('$id', '$content')");
答案 7 :(得分:-5)
$wpdb->query("insert into ".$table_name." (name, email, country, country, course, message, datesent) values ('$name','$email', '$phone', '$country', '$course', '$message', )");