954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP/MySql - Inserting Multiple Records

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

cancer10
Posting Whiz in Training
234 posts since Dec 2004
Reputation Points: 58
Solved Threads: 1
 

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

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

i think better be like this :

$query = "INSERT INTO table1 (col1)
          VALUES
          ('abc'),('ttt'), ('5t6'), ('gfd')";
mysql_query ($query,$connction);
sxz52
Newbie Poster
1 post since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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);
rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You