Question

I have the following code I use to insert form data into a single table in my data base.`

function insert_interests($uid, $interests) {
/* first, we'll delete any entries this user already has in the table */
    purge_lookup("jss_users_interests_table", $uid);
/* now create the sql insert query */
    global $db;
    $db->query(create_checkbox_query($interests, "jss_users_interests_table", $uid));
}
/* helper function for insert_interests(). removes all rows in $table with $uid */
function purge_lookup($table, $uid) {
    global $db;
    $db->query("DELETE FROM $table WHERE users_id = '".$db->escape($uid)."'");
}
/* helper function for insert_interests(). generates the actual SQL query */
function create_checkbox_query($arr, $table, $uid) {
    $q = "INSERT INTO $table (users_id, subcategories_id) VALUES";
    foreach ($arr as $check) {
    $q .=  " ( '$uid' , $check )" . ",";
    }
/* remove the last comma and return */  
return substr($q, 0, -1);
}`

On the heels of this code, I'd like to use the same form data paired with other data in another table to insert a new record into a different table. Here's the structure for both tables.

jss_users_interests_table

  • users_id
  • subcategories_id

jss_info_requests_table

  • users_id
  • provider_id
  • subcategories_id

jss_providers_assignments_table

  • provider_id
  • subcategories_id

So what I need to do after inserting the data into the jss_users_interests_table is insert the same data along with each subcategories_id's corresponding provider_id from the jss_provider_assignment_table into the jss_info_requests_table. Make sense? Am I borking it up and making it complicated?

Any help with the syntax would be awesome. Thanks!

Was it helpful?

Solution

This SQL might work.

 INSERT INTO jss_info_requests_table 
 (users_id, provider_id, subcategories_id)
SELECT a.users_id,
 b.provider_id, b.subcategories_id FROM
 jss_users_interests_table a,
 jss_providers_assignments_table b 
 WHERE a.subcategories_id =
 b.subcategories_id AND a.users_id =
 [USERID]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top