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

Read more here: MySQL query doesn’t work while using wpdb->prepare


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: