How to insert data using wpdb

2019-01-30 12:11发布

I have wriiten as following

$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);

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

8条回答
萌系小妹纸
2楼-- · 2019-01-30 12:39
global $wpdb;
$insert = $wpdb->query("INSERT INTO `front-post`(`id`, `content`) VALUES ('$id', '$content')");
查看更多
劳资没心,怎么记你
3楼-- · 2019-01-30 12:39

The recommended way (as noted in 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.

查看更多
登录 后发表回答