I want to insert records into multiple tables at one go, I am doing this in the following way, is this the correct way of doing? If not, what is the better way?

mysql_query("insert into table1 values('abc')");
mysql_query("insert into table2 values('ttt')");
mysql_query("insert into table3 values('5t6')");
mysql_query("insert into table4 values('ghy')");
mysql_query("insert into table5 values('gfd')");

Please help


Thanx

Recommended Answers

All 3 Replies

Yep. This is correct. But, its a good practice to have column name in your insert query. And also, Dont execute your query directly. Store the query in a variable and then execute it. This way, if you encounter any problem, you can easily print the query for manual testing.
ie.,

$query = "insert into table1 (col1) values ('ttt')";
mysql_query($query);

Cheers,
Naveen

i think better be like this :

$query = "INSERT INTO table1 (col1)
          VALUES
          ('abc'),('ttt'), ('5t6'), ('gfd')";
mysql_query ($query,$connction);
Member Avatar for rajarajan2017

Considered all tables are in a single database:

$query = "insert into table1 (col1) values ('data')";
mysql_query($query);
$query = "insert into table2 (col1) values ('data')";
mysql_query($query);
$query = "insert into table3 (col1) values ('data')";
mysql_query($query);
$query = "insert into table4 (col1) values ('data')";
mysql_query($query);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.