MySQL Insert into multiple tables? (Database normalization?)

前端 未结 8 2258
庸人自扰
庸人自扰 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:19

    try this

    $sql= " INSERT INTO users (username, password) VALUES('test', 'test') ";
    mysql_query($sql);
    $user_id= mysql_insert_id();
    if(!empty($user_id) {
    
    $sql=INSERT INTO profiles (userid, bio, homepage) VALUES($user_id,'Hello world!', 'http://www.stackoverflow.com');
    /* or 
     $sql=INSERT INTO profiles (userid, bio, homepage) VALUES(LAST_INSERT_ID(),'Hello   world!', 'http://www.stackoverflow.com'); */
     mysql_query($sql);
    };
    

    References
    PHP
    MYSQL

提交回复
热议问题