Inserting Multiple Records into the DB

Posted on 2009-04-15
Last Modified: 2012-05-06
Hi Experts,

I am currently coding something which allows users to create an announcement in their group. My idea is that the announcement will be shown on the group page for each member and they can click the cross to remove the announcement.

For this, I'm using db tables to insert a record for each userid with the same message and then when they delete the announcement it removes the record from the database.

Now, I have a query which gets me all the user ID's I want but i'm struggling on finding out how to insert multiple records, the code below is what I have so far but it's only inserting 1 record (the first userid from the query which gets the IDs):-

I think I may need a foreach statement but not sure how to implement it?

Thanks in advance
//Get the User ID's

$query='SELECT cm.member_id FROM members AS cm  WHERE cm.group_id = '.$id.' AND NOT IN ("'.$username.'") AND cm.deleted = 0';




		//Insert a record for each member so they can dismiss the record

		$query='INSERT INTO announcements (group_id,member_id,subject,description,added_by,date_added)

				VALUES ('.$id.','.$list[0].',"'.$subject.'","'.$description.'","'.$added_by.'",'.$date_added.');';




Open in new window

Question by:EzEApostle
  • 2
  • 2
LVL 30

Expert Comment

ID: 24147543

Accepted Solution

QualitySoftwareDevelopment earned 500 total points
ID: 24147612
if you need more inserts in one line you can do like this:

$sql = "INSERT INTO table_name (field1, filed2, field3)
  ($data1, $data2, $data3),
  ($data1, $data2, $data3),
  ($data1, $data2, $data3),
  ($data1, $data2, $data3)";

Author Comment

ID: 24147644
I checked that thread from google but it doesnt help as I'm not using a form
LVL 30

Expert Comment

ID: 24148506
what are you using php?

Author Closing Comment

ID: 31570403

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
wordpress url rewriting plugin 5 41
inserting while loop results 2 27
Php pie charts 3 26
test if query has no results 2 19
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now