Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Inserting Multiple Records into the DB

Posted on 2009-04-15
Medium Priority
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 cm.name 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

Accepted Solution

QualitySoftwareDevelopment earned 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

580 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