943,650 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3839
  • PHP RSS
Mar 20th, 2008
0

PHP/MySql - Inserting Multiple Records

Expand Post »
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?



PHP Syntax (Toggle Plain Text)
  1. mysql_query("insert into table1 values('abc')");
  2. mysql_query("insert into table2 values('ttt')");
  3. mysql_query("insert into table3 values('5t6')");
  4. mysql_query("insert into table4 values('ghy')");
  5. mysql_query("insert into table5 values('gfd')");



Please help


Thanx
Reputation Points: 58
Solved Threads: 1
Posting Whiz in Training
cancer10 is offline Offline
234 posts
since Dec 2004
Mar 20th, 2008
0

Re: PHP/MySql - Inserting Multiple Records

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.,
php Syntax (Toggle Plain Text)
  1. $query = "insert into table1 (col1) values ('ttt')";
  2. mysql_query($query);
Cheers,
Naveen
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Jul 3rd, 2010
0
Re: PHP/MySql - Inserting Multiple Records
i think better be like this :
PHP Syntax (Toggle Plain Text)
  1. $query = "INSERT INTO table1 (col1)
  2. VALUES
  3. ('abc'),('ttt'), ('5t6'), ('gfd')";
  4. mysql_query ($query,$connction);
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sxz52 is offline Offline
1 posts
since Jul 2010
Jul 5th, 2010
0
Re: PHP/MySql - Inserting Multiple Records
Considered all tables are in a single database:
PHP Syntax (Toggle Plain Text)
  1. $query = "insert into table1 (col1) values ('data')";
  2. mysql_query($query);
  3. $query = "insert into table2 (col1) values ('data')";
  4. mysql_query($query);
  5. $query = "insert into table3 (col1) values ('data')";
  6. mysql_query($query);
  7. $query = "insert into table4 (col1) values ('data')";
  8. mysql_query($query);
Reputation Points: 167
Solved Threads: 239
Nearly a Posting Virtuoso
rajarajan07 is offline Offline
1,445 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: finding a match
Next Thread in PHP Forum Timeline: Which function is used to add text in array from a text file?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC