Hallo, ich versuche, Daten in die Datenbank einzufügen, aber $ wpdb-> insert zwei Zeilen einfügen.
mein Code zum Einfügen von Daten ist
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);
}
größe von $ emails ist 1 (wenn ich eine email Identifikation einfüge).
Wo mache ich Fehler bitte lass es mich wissen.
Versuchen Sie so etwas
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;
}
Ich gehe davon aus, dass $email
ein Array ist, das einige Daten enthält. Wenn Sie das gesamte Array speichern möchten, sollten Sie serialize()
das Array.