插入两个相关表格

tbl_account
username | password | status

tbl_data
username | name | address | phone

how can i insert data into both tables with php? I tried with query

INSERT INTO tbl_account(username, password, status)
OUTPUT INSERTED.'bobo', 'Bobo Ali','Markt St. 26', '0979877'
INTO tbl_data(username, name, address, phone)
VALUES  ('bobo','bobo123','manager');

but it doesn't work at all. The process is success but the table is remain empty.

You will need seperate queries for inserting the same data into multiple tables.

If you are having trouble with that, the process is simple:

  1. Use the query to insert the data into the first table
  2. Free the last result using mysqli_free_result() (or mysql_free_result() if you are using the MySQL API.
  3. Reuse the same query you used in step 1. to insert the data into the second table

Code:

<?php

INSERT INTO tbl_account(username, password, status)
VALUES ('bobo', 'Bobo Ali','Markt St. 26', '0979877');

// Free the result here

INSERT INTO tbl_data(username, name, address, phone)
VALUES  ('bobo','bobo123','manager');

// Free the result again, so you can use the query again if needed

?>

you can only do with 2 seperate queries

INSERT INTO tbl_account(username, password, status)
 VALUES ('bobo', 'Bobo Ali','Markt St. 26', '0979877)'

Insert INTO tbl_data(username, name, address, phone)
VALUES  ('bobo','bobo123','manager');

Use transactions. Alter your database like in the example and make userid primary key (auto increment)

tbl_account
userid | username | password | status

tbl_data
userid | username | name | address | phone

BEGIN;
INSERT INTO tbl_account(username, password, status)
  VALUES ('bobo','bobo123','manager');
INSERT INTO  tbl_data(username, name, address, phone)
  VALUES(LAST_INSERT_ID(),'bobo', 'Bobo Ali','Markt St. 26', '0979877');
COMMIT;

Check also this example if you want an example of php-MySQL(transaction) implementation. PHP + MySQL transactions examples

Yes, you need to have two separate queries. But the answers are a little bit misleading because the table tbl_account has only three columns whereas the provided code inserts values for four columns.

So it should be:

INSERT INTO tbl_account(username, password, status)
VALUES  ('bobo','bobo123','manager');

INSERT INTO tbl_data(username, name, address, phone)
VALUES ('bobo', 'Bobo Ali','Markt St. 26', '0979877');

Or you can write a procedure which these two inserts does.