Hi there,
I am currently trying to use the following code to save entries from my form (ID 3) to a custom table in my database:
register_activation_hook( __FILE__, 'endo_create_custom_table' );
function endo_create_custom_table() {
global $wpdb;
$table_name = "wp_reservations";
$sql = "CREATE TABLE $table_name(
id mediumint(9) NOT NULL AUTO_INCREMENT,
entry_id VARCHAR(50) NOT NULL,
date datetime,
email VARCHAR(50) CHARACTER SET utf8,
guest VARCHAR(50) CHARACTER SET utf8,
guest_amount VARCHAR(50) CHARACTER SET utf8,
date_reservation VARCHAR(50) CHARACTER SET utf8,
menu_own VARCHAR(50) CHARACTER SET utf8,
menu_gast_1 VARCHAR(50) CHARACTER SET utf8,
menu_gast_2 VARCHAR(50) CHARACTER SET utf8,
menu_gast_3 VARCHAR(50) CHARACTER SET utf8,
menu_gast_4 VARCHAR(50) CHARACTER SET utf8,
menu_gast_5 VARCHAR(50) CHARACTER SET utf8,
menu_gast_6 VARCHAR(50) CHARACTER SET utf8,
menu_gast_7 VARCHAR(50) CHARACTER SET utf8,
menu_gast_8 VARCHAR(50) CHARACTER SET utf8,
menu_gast_9 VARCHAR(50) CHARACTER SET utf8,
menu_gast_10 VARCHAR(50) CHARACTER SET utf8,
UNIQUE KEY id(id)
) COLLATE utf8_general_ci;";
require_once(ABSPATH . '/wp-admin/includes/upgrade.php');
dbDelta($sql);
}
add_action('gform_after_submission_3', 'endo_add_entry_to_db', 10, 2);
function endo_add_entry_to_db($entry, $form) {
$source = $entry['source_url'];
$email = $entry[1];
$guest = $entry[4];
$guest_amount = $entry[6];
$date_reservation = $entry[12];
$menu_own = $entry[13];
$menu_gast_1 = $entry[15];
$menu_gast_2 = $entry[29];
$menu_gast_3 = $entry[30];
$menu_gast_4 = $entry[31];
$menu_gast_5 = $entry[32];
$menu_gast_6 = $entry[33];
$menu_gast_7 = $entry[35];
$menu_gast_8 = $entry[34];
$menu_gast_9 = $entry[36];
$menu_gast_10 = $entry[37];
global $wpdb;
// add form data to custom database table
$wpdb->insert(
'wp_reservations',
array(
'source_url' => $source,
'email' => $email,
'guest' => $guest,
'guest_amount' => $guest_amount,
'date_reservation' => $date_reservation,
'menu_own' => $menu_own,
'menu_gast_1' => $menu_gast_1,
'menu_gast_2' => $menu_gast_2,
'menu_gast_3' => $menu_gast_3,
'menu_gast_4' => $menu_gast_4,
'menu_gast_5' => $menu_gast_5,
'menu_gast_6' => $menu_gast_6,
'menu_gast_7' => $menu_gast_7,
'menu_gast_8' => $menu_gast_8,
'menu_gast_9' => $menu_gast_9,
'menu_gast_10' => $menu_gast_10,
'date' => current_time( 'mysql' )
)
);}
However, it doesn’t seem to work for me. I set up a custom plugin and the table is created within the database, however if I submit a new entry, nothing happens in the database.
Could you help me please?
Thank you!