954,174 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Trying to insert into 2 tables with 1 shared id

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.

$sql_insert1 = "INSERT INTO `agent_users`
  			(`user_name`,
			 `password`,
			 `primary_email`,
			 `secondary_email`,
			 `join_date`,
			 `last_accessed`,
			 `activation_code`,
			 `first_name`,
			 `last_name`,
			 `license_num`,
			 `agent_dob`
			 )
			VALUES
			('$data[user_name]',
			 '$md5pass',
			 '$usr_email',
			 '$data[secondary_email]',
			 'now()',
			 'date()',
			 '$activ_code',
			 '$data[first_name]',
			 '$data[last_name]',
			 '$data[license_num]',
			 '$dob'
			 )";
	
	$sql_insert2 = "INSERT INTO `agent_company`
			(`exp_age_yr`,
			 `exp_age_mo`,
			 `exp_sales`,
			 `exp_sSales`,
			 `exp_reo`,
			 `exp_propmngt`,
			 `exp_foreclosures`,
			 `exp_lMods`,
			 `exp_bpo`,
			 `exp_commreal`,
			 `company_name`,
			 `company_address`,
			 `company_suite`,
			 `company_city`,
			 `company_state`,
			 `company_zip`,
			 `company_tel`,
			 `company_tel_ext`,
			 `company_fax`,
			 `company_web` 
			)
		    VALUES
			('$data[exp_age_yr]',
			 '$data[exp_age_mo]',
			 '$chkbx_results[exp_sales]',
			 '$chkbx_results[exp_sSales]',
			 '$chkbx_results[exp_reo]',
			 '$chkbx_results[exp_propmngt]',
			 '$chkbx_results[exp_foreclosures]',
			 '$chkbx_results[exp_lMods]',
			 '$chkbx_results[exp_bpo]',
			 '$chkbx_results[exp_commreal]',
			 '$data[company_name]',
			 '$data[company_address]',
			 '$data[company_suite]',
			 '$data[company_city]',
			 '$data[ompany_state]',
			 '$data[company_zip]',
			 '$data[company_tel]',
			 '$data[company_tel_ext]',
			 '$data[company_fax]',
			 '$data[company_web]'
			)";

	mysql_query($sql_insert1,$link) or die("Insertion Failed:" . mysql_error());
	$user_id = mysql_insert_id($link);  
	$md5_id = md5($user_id);
	mysql_query("UPDATE `agent_users` SET md5_id='$md5_id' WHERE id='$user_id'");
	
	mysql_query($sql_insert2,$link2) or die("Insertion Failed:" . mysql_error());
	$user_id2 = mysql_insert_id($link);  
	//$new_id = mysql_query("SELECT `id` FROM `agent_users` WHERE id='$user_id'");
	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

s2xi
Newbie Poster
13 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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

"insert into agent_company (agent_id,exp_age_yr, exp_age_month.........)
values ('$user_id}',$data[exp_age_yr]', '$data[exp_age_mo]',)";

NOW AGENT_ID COLUMN IN BOTH TABLES IS A LINK BETWEEN TWO TABLES.

urtrivedi
Nearly a Posting Virtuoso
1,306 posts since Dec 2008
Reputation Points: 257
Solved Threads: 270
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: