Jump to content

Multiple row insert


public-image

Recommended Posts

Hi I haven't really looked around for help on this code and so would be hopeful that someone from PHP Freaks could help me out.

 

Okay I have a table called badges which on registration I should have put a MYSQL insert rows into, but I forgot and now it doesn't have the rows for the users that are currently registered. Is there a PHP code I could do that would select every user in my 'users' table and be able to create a row (or two) for every individual counted in the 'users table' therefore the rows would then be inserted for those who are registered...

 

Thanks

Link to comment
Share on other sites

Hi I haven't really looked around for help on this code and so would be hopeful that someone from PHP Freaks could help me out.

 

Okay I have a table called badges which on registration I should have put a MYSQL insert rows into, but I forgot and now it doesn't have the rows for the users that are currently registered. Is there a PHP code I could do that would select every user in my 'users' table and be able to create a row (or two) for every individual counted in the 'users table' therefore the rows would then be inserted for those who are registered...

 

Thanks

 

Let me see if I get you right...you wrote a script that signs up users and forgot to put in an insert, so the users aren't being registered?

Link to comment
Share on other sites

Not quite, my sign up script would add a row for each member to users right... but you might wish to add certain information used from your registration page to other tables too... like the table 'badges' and I forgot to add this and so the rows for when a user registers have not been being added to the table 'badges'. (But yes they are being registered) although my question is, is there a way to take all the registered users in some sort of query and make the code add the rows for every user registered into the table 'badges'.

 

E.g. I have 3 members is there a way that a PHP code could collect the amount of users you have in a table and possibly their username and in a query add a row into another table using the data of the user it collects

 

E.g.

Users -

id 1 - username: Bob

id 2 - username: Sally

id 3 - username: CJ

 

>>> Collects rows (3) >>> Inserts row into table 'badges' using information collected from the a user or in my case insert a row for every user in the 'users' table and put 1 row into 'table' specific for this user

 

So when you do a SELECT you would say in table badges where username ='Bob'

every user registered would then have a row specific to them...

 

Hope this helped

 

 

Link to comment
Share on other sites

Yes, it is absolutely possible. HOW you do it will depend on exactly what you need to do and the complexity involved. For example you might be able to accomplish what you need by running a simple query in PHPMYADMIN. Or you might need to run a query for all the current records and run them through a PHP processing script first.

 

Here is a very basic example:

 

The table badges requires a single record associated with each user. The feild user_id is used in both tables to associate them.

INSERT INTO badges (user_id, badge_value)
VALUES (SELECT user_id, 'default'
        FROM users u
        LEFT JOIN badges b ON u.user_id = b.user_id
        WHERE b.user_id IS NULL
       )

 

That willinsert a new record with the user ID and a default value of "default" for every user that does not currently have an associated record in the badges table.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.