agr8lemon Posted April 22, 2010 Share Posted April 22, 2010 Hello everyone, I know there are a lot of articles about inserting into joined tables, but they all seem to do with auto incrementing both Keys at the same time. Mine is different and I'm hoping someone can help. I have two tables with a PK/FK relationship Emp_tbl ---------- ID (PK, int, not null, autoinc) Depart_ID (FK, int not null) Depart_tbl ---------- Depart_ID(PK, int, not null, autoinc) On my form I have a drop down that pulls the data from B to a drop-down that creates the value of the dropdown the integer of Depart_ID as follows: $sql_depart="SELECT Depart_Name, Depart_ID, Depart_Facility FROM dbo.Depart_tbl WHERE Disabled = '0' ORDER BY Depart_Name ASC"; $departResult=odbc_exec($dept_conn, $sql_depart); $departOptions=""; while($departRows=odbc_fetch_array($departResult)){ $departId=$departRows["Depart_ID"]; $Departments=$departRows['Depart_Name']; $departOptions.="<OPTION VALUE=\"$departId\">".$Departments.'</option>'; } odbc_close($dept_conn); So what I want to happen is to insert the data data into the Emp_tbl along with the proper Depart_ID for Depart_tbl (Keep in mind I don't want to change the Depart_tbl at all, just grab the PK value of and put that into the Emp_tbl for the join). Then this is my insert page: $_POST['Depart_ID'] = (int)$Depart_ID; ///Define our insert $sql="INSERT INTO dbo.Emp_tbl (Emp_First, Emp_Last, Emp_Title, Emp_Phone, Emp_Cell, Emp_Pager, Emp_Fax, Depart_ID) VALUES ('$_POST[Emp_First]','$_POST[Emp_Last]','$_POST[Emp_Title]','$_POST[Emp_Phone]','$_POST[Emp_Cell]','$_POST[Emp_Pager]','$_POST[Depart_Fax]','$Depart_ID')"; ///Make the insert $result=odbc_exec($emp_conn, $sql); The error I get is: Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC SQL Server Driver] The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Emp_Info_Emp_Department". The conflict occurred in database "PhoneDir", table "dbo.Depart_tbl", column 'Depart_ID'., SQL state 23000 in SQLExecDirect in Does this make sense? Can anyone help me? Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/199439-phpmssql-insert-into-joined-tables/ Share on other sites More sharing options...
Brian Swan Posted April 22, 2010 Share Posted April 22, 2010 I could be totally wrong here, but isn't this line a problem? $_POST['Depart_ID'] = (int)$Depart_ID; Don't you want this? $Depart_ID = $_POST['Depart_ID']; -Brian Quote Link to comment https://forums.phpfreaks.com/topic/199439-phpmssql-insert-into-joined-tables/#findComment-1046737 Share on other sites More sharing options...
agr8lemon Posted April 23, 2010 Author Share Posted April 23, 2010 Wow, Thank you. I was trying to do $Depart_ID = (int)$_POST['Depart_ID']; because it wanted to make sure it was an integer and my fingers and brain must be tired. Anyways, it works now thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/199439-phpmssql-insert-into-joined-tables/#findComment-1046777 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.