943,154 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Unsolved
  • Views: 1667
  • MySQL RSS
Feb 7th, 2010
0

Trying to insert into 2 tables with 1 shared id

Expand Post »
Hey, so I have a form and i want to break down the form into 2 sections when inserting into my database. I have made 2 tables to hold my values.

this is my SQL statements.

MySQL Syntax (Toggle Plain Text)
  1. $sql_insert1 = "INSERT INTO `agent_users`
  2. (`user_name`,
  3. `password`,
  4. `primary_email`,
  5. `secondary_email`,
  6. `join_date`,
  7. `last_accessed`,
  8. `activation_code`,
  9. `first_name`,
  10. `last_name`,
  11. `license_num`,
  12. `agent_dob`
  13. )
  14. VALUES
  15. ('$data[user_name]',
  16. '$md5pass',
  17. '$usr_email',
  18. '$data[secondary_email]',
  19. 'now()',
  20. 'date()',
  21. '$activ_code',
  22. '$data[first_name]',
  23. '$data[last_name]',
  24. '$data[license_num]',
  25. '$dob'
  26. )";
  27.  
  28. $sql_insert2 = "INSERT INTO `agent_company`
  29. (`exp_age_yr`,
  30. `exp_age_mo`,
  31. `exp_sales`,
  32. `exp_sSales`,
  33. `exp_reo`,
  34. `exp_propmngt`,
  35. `exp_foreclosures`,
  36. `exp_lMods`,
  37. `exp_bpo`,
  38. `exp_commreal`,
  39. `company_name`,
  40. `company_address`,
  41. `company_suite`,
  42. `company_city`,
  43. `company_state`,
  44. `company_zip`,
  45. `company_tel`,
  46. `company_tel_ext`,
  47. `company_fax`,
  48. `company_web`
  49. )
  50. VALUES
  51. ('$data[exp_age_yr]',
  52. '$data[exp_age_mo]',
  53. '$chkbx_results[exp_sales]',
  54. '$chkbx_results[exp_sSales]',
  55. '$chkbx_results[exp_reo]',
  56. '$chkbx_results[exp_propmngt]',
  57. '$chkbx_results[exp_foreclosures]',
  58. '$chkbx_results[exp_lMods]',
  59. '$chkbx_results[exp_bpo]',
  60. '$chkbx_results[exp_commreal]',
  61. '$data[company_name]',
  62. '$data[company_address]',
  63. '$data[company_suite]',
  64. '$data[company_city]',
  65. '$data[ompany_state]',
  66. '$data[company_zip]',
  67. '$data[company_tel]',
  68. '$data[company_tel_ext]',
  69. '$data[company_fax]',
  70. '$data[company_web]'
  71. )";
  72.  
  73. mysql_query($sql_insert1,$link) OR die("Insertion Failed:" . mysql_error());
  74. $user_id = mysql_insert_id($link);
  75. $md5_id = MD5($user_id);
  76. mysql_query("UPDATE `agent_users` SET md5_id='$md5_id' WHERE id='$user_id'");
  77.  
  78. mysql_query($sql_insert2,$link2) OR die("Insertion Failed:" . mysql_error());
  79. $user_id2 = mysql_insert_id($link);
  80. //$new_id = mysql_query("SELECT `id` FROM `agent_users` WHERE id='$user_id'");
  81. mysql_query("UPDATE `agent_company` SET company_id='$user_id'");

I was trying to insert my values into my agent_users table and then also insert their company information into my agent_company table.

My logic was in order for me to connect these tables to one member or user what ever you want to call it. to be connected by the agent_user `id` field, which is set to auto_increment.

What would I need to do get the `id` number to match up with the agent_company `id` number? hmmm, both id's are set to auto and primary keys.

Also, i made a company_id and thought of setting it up as a foreign key but I don't really understand foreign keys just yet... so do i need it right now?? or its something that i must have to connect both these tables to one user/member?

any help is appreciated!

thx
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
s2xi is offline Offline
13 posts
since Jan 2010
Feb 8th, 2010
0
Re: Trying to insert into 2 tables with 1 shared id
Follow steps given below
1) Keep same column in both table with same data type say AGENT_ID.
2) keep this as primary key in both tables
3) In agent_users table keep it as auto_increment, DO NOT set agent_id as auto_increment in agent_company table.
4) In your mysql code first insert into agent_users table
5) save id in $user_id variable using mysql_insert_id($link)
6) now insert into agent_comapany table like
MySQL Syntax (Toggle Plain Text)
  1. "insert into agent_company (agent_id,exp_age_yr, exp_age_month.........)
  2. values ('$user_id}',$data[exp_age_yr]', '$data[exp_age_mo]',)";
NOW AGENT_ID COLUMN IN BOTH TABLES IS A LINK BETWEEN TWO TABLES.
Reputation Points: 245
Solved Threads: 259
Nearly a Posting Virtuoso
urtrivedi is offline Offline
1,248 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: I should know how to do this
Next Thread in MySQL Forum Timeline: SQL SELECT question.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC