1

I need to insert into two tables at the same time so I'm using BEGIN and COMMIT in the query using $wpdb->query() and $wpdb->prepare() but unfortunately it doesn't work

Testing by [ die( $wpdb->last_error ); ],

Generates [ WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO TABLE_NAME ( name ) VALUES ( 'xxxxxx' ); at line 2] ]

P.S. I copied the printed query and paste it in mySQL Query window and IT WORKS!!!

$wpdb->query( $wpdb->prepare(
       "BEGIN;
       INSERT INTO {$tbl_accounts} ( name ) 
           VALUES ( %s );
       SELECT LAST_INSERT_ID() INTO @last_seller_id;
       INSERT INTO {$tbl_apps} ( seller_id, slug, name, token ) 
           VALUES ( @last_seller_id, %s, %s, %s );
       COMMIT;",
       $seller_name,
       $app_name_sanitized, 
       $app_name, 
       $token_key ) 
);
Hady Shaltout
  • 606
  • 1
  • 9
  • 22

0 Answers0