Jump to content

Inserting Records From Another Table/Updating Table Help


jimmyoneshot

Recommended Posts

Hello. I'm new to the forums and was wondering if any of you guys could please help me out. I have a project which allows users to favourite links to websites and also allows my client to add in new links to websites and update their details. The table which contains all the links which my client has added is called links and contains the values linkid (primary key in this table and each linkid is unique as it auto-increments), categoryid, label, icon and url. I then have a table called userlinks containing the values userid (NOT primary and NOT unique as this table is intended not to have a primary key), linkid, categoryid, label, icon, url and favourited (default value for favourited is 'N'). When the user creates a new account it creates a new userid for that new account in my users table. '$param_userid' is what I've assigned this value to in my php code as the logged in user. What I want is for each time a user creates an account a copy of all of the records in all of the columns in the links table is copied into the corresponding columns in the userlinks table as well as the values of $param_userid (the logged in userid) and favourited ('N') being applied for each record. Can anyone help me out with the code for this? I'm very inexperienced at using php.  :'(

 

I also then want a query which when a user logs in with an existing account all of the data from the links table is copied over in the same way except this time it overwrites any records which the user with the logged in userid has, based on linkid and also inserts any new records which may have been added to the links table  that they haven't had since they created their account (again with the logged in user id '$param_userid' and 'N' for favourited values added for each record).

 

I hope this makes sense and I'd be very grateful if anyone could help me with these 2 queries.  :shy:

Well, it seems quite stupid to do that. You're replicating the data twice. Why not just have a column that points to linkid in links? That way, you already have all the data without replicating it. All you have to do is query the links table with the linkid and since it's a primary key, there should be one.

Sorry what I mean is the links table will contain all of the links which the client adds and the userlinks table will contain a copy of all of the links for specific users . For example lets say if there are 2 users (1 with userid 1 and one with userid 2) and the links table contains 5 links. Then the links table would look something like:-

 

    |linkid|            |categoryid|      |label|          |icon|                  |url|

      1                      3                  Yahoo            yahoo.jpg            yahoo.com

      2                      4                  Play              play.jpg              play.com

      3                      1                  Bloomberg    bloomberg.jpg    bloomberg.com

      4                      2                  SkySports      skysports.jpg      skysports.com

      5                      6                  Facebook      facebook.jpg      facebook.com

 

Then the userlinks table may look something like this:-

 

|userid|            |linkid|            |categoryid|      |label|          |icon|                  |url|                    |favourited|

    1                  1                      3                  Yahoo            yahoo.jpg            yahoo.com                    N

    1                  2                      4                  Play              play.jpg              play.com                        N

    1                  3                      1                  Bloomberg    bloomberg.jpg    bloomberg.com            N

    1                  4                      2                  SkySports      skysports.jpg      skysports.com              N

    1                  5                      6                  Facebook      facebook.jpg      facebook.com                N

    2                  1                      3                  Yahoo            yahoo.jpg            yahoo.com                    N

    2                  2                      4                  Play              play.jpg              play.com                        N

    2                  3                      1                  Bloomberg    bloomberg.jpg    bloomberg.com            N

    2                  4                      2                  SkySports      skysports.jpg      skysports.com              N

    2                  5                      6                  Facebook      facebook.jpg      facebook.com              N

 

See what I mean?

Ah actually I see what you mean now that I've thought about it, I could just have 3 colums in the userlinks table userid linkid and favourited.

 

So what would be the query to copy all of the linkid records from the links table and apply the value of the logged in userid ('$param_userid') and the value of N to each of these records in the userlinks table? So that if a new user creates an account it would insert this (based on the example that there are only 5 records in the links table):-

 

|userid|            |linkid|            |favourited|     

    1                  1                      N                 

    1                  2                      N                 

    1                  3                      N                 

    1                  4                      N               

    1                  5                      N               

Actually come to think of it I do need all of the records to be in the new table the reason for this is these records will be used to populate data in flex. It's hard to explain but it seems I do indeed need to have all the records from the userslinks table copied over into the links table with the userid and favourited applied to each specific record.

 

Has anyone got a clue on this or is it too hard or just not possible?  :shrug:

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.