1

I need to run an insert and a simultaneous update, but I'm using only insert test to catch errors.

The PDO does not report the error when I define a correct and an incorrect table, but if I run a query to select one incorrect table the error is reported.

I do not have enough knowledge to solve this problem, i really need to execute two queries together Thank you.

$db-> setAttribute( PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION );

try{
    $stmt = $db-> prepare( "INSERT INTO test ( name ) values( 'new name' );
                            INSERT INTO xast ( xame ) values( 'new name' );" );
    $stmt->execute();
}catch( PDOException $e ){
    print_r( $e );
}


try{
    $stmt = $db-> prepare( "INSERT INTO xast ( xame ) values( 'new name' );" );
    $stmt->execute();
}catch( PDOException $e ){
    print_r( $e );
}
**Column not found: 1054 Unknown column 'xame'**
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
Papa Charlie
  • 625
  • 8
  • 31

1 Answers1

1

there is no point in preparing multiple queries in your case.

First, prepared statements invented especially for tins case: prepare once, execute many. So, you can prepare this way

INSERT INTO test (name) values(?)

and then execute as many times as many inserts you have

Next. For the INSERT query Mysql supports multiple VALUES clause in one query, like

INSERT INTO test (name) values ('new name' ),('name'),(...)

and so on.

If you need to update after insert - no problem too.
Just run 2 separate queries - one INSERT and one UPDATE. There will be not a single problem.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • I said I need to insert and update, not an insert with many fields. i need insert and an update, but apparently not working correctly, thank you – Papa Charlie Jan 15 '13 at 07:05