Solved

Inserting Multiple Records into the DB

Posted on 2009-04-15
5
230 Views
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';
$result=mysql_query($query);
 
		while($list=mysql_fetch_array($result,MYSQL_NUM))
		{
		//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.');';
		$result=mysql_query($query);
		
		}

Open in new window

0
Comment
Question by:EzEApostle
  • 2
  • 2
5 Comments
 
LVL 30

Expert Comment

by:IanTh
ID: 24147543
0
 
LVL 3

Accepted Solution

by:
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)
  VALUES
  ($data1, $data2, $data3),
  ($data1, $data2, $data3),
  ($data1, $data2, $data3),
  ($data1, $data2, $data3)";
0
 
LVL 1

Author Comment

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

Expert Comment

by:IanTh
ID: 24148506
what are you using php?
0
 
LVL 1

Author Closing Comment

by:EzEApostle
ID: 31570403
owih
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

816 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

10 Experts available now in Live!

Get 1:1 Help Now