Solved

Insert multiple rows based on number of clients from another table

Posted on 2010-09-06
2
447 Views
Last Modified: 2013-12-13
Using php and mysql,

I have a table called "certificates" and another table called "clients". I'd like to issue a certificate to every client in the database based on the existance of their user_id within the client table.

Basic logic being

Grab all unique user_ids from table clients

Insert into certificates the certificate value (from POST data) and a new row for every unique user_id

So the end result is if there are 10 user_ids in clients, 10 certificates will be issued... 1 for each client. I do store the user_id in the certificate table for obvious reasons.
0
Comment
Question by:Russelauto
2 Comments
 
LVL 6

Accepted Solution

by:
nasirbest earned 500 total points
ID: 33611852
Your question is not clear. what happen if you POST certificate value twice
To keep things simple, I assume you will not do that, or on another post 10 more certificates entries will be created for clients.

here is code
$cert   = $_POST['certificate'];

// assume certificate table has two columns user_id and cert_value
$result = mysql_query("INSERT INTO certificate (user_id, cert_value) SELECT DISTINCT user_id, '$cert' FROM clients");

Open in new window

0
 

Author Closing Comment

by:Russelauto
ID: 33612667
Thank you for your quick response.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
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…
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…
The viewer will learn how to count occurrences of each item in an array.

707 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

13 Experts available now in Live!

Get 1:1 Help Now