MySQL Insert into multiple tables? (Database normalization?)

前端 未结 8 2412
庸人自扰
庸人自扰 2020-11-22 01:56

I tried searching a way to insert information in multiple tables in the same query, but found out it\'s impossible? So I want to insert it by simpl

8条回答
  •  庸人自扰
    2020-11-22 02:25

    For PDO You may do this

    $stmt1 = "INSERT INTO users (username, password) VALUES('test', 'test')"; 
    $stmt2 = "INSERT INTO profiles (userid, bio, homepage) VALUES('LAST_INSERT_ID(),'Hello world!', 'http://www.stackoverflow.com')";
    
    $sth1 = $dbh->prepare($stmt1);
    $sth2 = $dbh->prepare($stmt2);
    
    BEGIN;
    $sth1->execute (array ('test','test'));
    $sth2->execute (array ('Hello world!','http://www.stackoverflow.com'));
    COMMIT;
    

提交回复
热议问题