您好,我试图在数据库中插入数据,但$wpdb->插入插入两行。
我的插入数据代码是
global $wpdb;
$tablename = $wpdb->prefix . \'<TABLE NAME>\';
for ($i = 0; $i < sizeof($emails); $i++) {
$data = array("post_id" => $post_id,
"user_id" => $user_id,
"email" => $emails[$i],
"status" => 1
);
$wpdb->insert($tablename, $data);
}
$电子邮件的大小为1(如果我插入一个电子邮件id)。
我哪里出错了请告诉我。
SO网友:Steven
试试这样的
function insert_data_into_table () {
global $wpdb;
$table_name = $wpdb->prefix . "your_table_name";
//Check if table exists
if($wpdb->get_var("show tables like \'$table_name\'") != $table_name) :
//if not, create the table
$sql = "CREATE TABLE " . $table_name . " (
(...)
) ENGINE=InnoDB;";
require_once(ABSPATH . \'wp-admin/includes/upgrade.php\');
dbDelta($sql);
else:
foreach($emails as $mail):
$insert = "INSERT INTO " . $table_name . "
(post_id, post_id, email, post_id)
VALUES ($post_id, $user_id, email[\'content\'], 1);"
$results = $wpdb->query( $insert );
endforeach;
endif;
}
我想
$email
数组是否包含一些数据?如果要保存整个阵列,则应
serialize()
阵列。