big-dog1965 Posted January 11, 2009 Share Posted January 11, 2009 With a form that inserts data into mysql how do I get it to insert into 2 differant table in this excample it doesnt insert anything into the main table an does insert the data into the 2nd tabel. If I delete $query="insert into Class_Alias_Cla (Cla_Cl_PK,Cla_F_Name) values ('".$Cl_PK."','".$ClFName."')"; then it inserts into main table $query="insert into Class_Cl (Cl_Act,Cl_PK,Cl_S_Name,Cl_F_Name,Cl_Notes,Cl_R1,Cl_R2,Cl_R3,Cl_R4,Cl_R5,Cl_R6,Cl_R7,Cl_R8,Cl_R9,Cl_R10,CL_Unknown) values ('".$ClAct."','".$ClPK."','".$ClSName."','".$ClFName."','".$ClNotes."','".$ClR1."','".$ClR2."','".$ClR3."','".$ClR4."','".$ClR5."','".$ClR6."','".$ClR7."','".$ClR8."','".$ClR9."','".$ClR10."','".$CLUnknown."')"; $query="insert into Class_Alias_Cla (Cla_Cl_PK,Cla_F_Name) values ('".$Cl_PK."','".$ClFName."')"; mysql_query($query); Quote Link to comment https://forums.phpfreaks.com/topic/140407-insert-results-from-form-with-php/ Share on other sites More sharing options...
sKunKbad Posted January 11, 2009 Share Posted January 11, 2009 You just use 2 seperate INSERT queries. Quote Link to comment https://forums.phpfreaks.com/topic/140407-insert-results-from-form-with-php/#findComment-734840 Share on other sites More sharing options...
RestlessThoughts Posted January 11, 2009 Share Posted January 11, 2009 Try putting "mysql_query($query);" after the first query as well as after the second. Right now you're replacing the first query with the second. Quote Link to comment https://forums.phpfreaks.com/topic/140407-insert-results-from-form-with-php/#findComment-734843 Share on other sites More sharing options...
big-dog1965 Posted January 11, 2009 Author Share Posted January 11, 2009 Thank you RestlessThoughts The explanation on why it didnt work was great I learned something Thanks Didnt see where to put post resolved but it is Quote Link to comment https://forums.phpfreaks.com/topic/140407-insert-results-from-form-with-php/#findComment-734857 Share on other sites More sharing options...
big-dog1965 Posted January 11, 2009 Author Share Posted January 11, 2009 Same code but one thing where the 2nd table Class_Alias_Cla (Cla_Cl_PK is I want $ClPK from the 1st table inserted into there but it dosent might note that $ClPK is auto_increment if that matters? $query="insert into Class_Cl (Cl_Act,Cl_PK,Cl_S_Name,Cl_F_Name,Cl_Notes,Cl_R1,Cl_R2,Cl_R3,Cl_R4,Cl_R5,Cl_R6,Cl_R7,Cl_R8,Cl_R9,Cl_R10,CL_Unknown) values ('".$ClAct."','".$ClPK."','".$ClSName."','".$ClFName."','".$ClNotes."','".$ClR1."','".$ClR2."','".$ClR3."','".$ClR4."','".$ClR5."','".$ClR6."','".$ClR7."','".$ClR8."','".$ClR9."','".$ClR10."','".$CLUnknown."')"; mysql_query($query); $query="insert into Class_Alias_Cla (Cla_Cl_PK,Cla_F_Name) values ('".$ClPK."','".$ClFName."')"; mysql_query($query); Quote Link to comment https://forums.phpfreaks.com/topic/140407-insert-results-from-form-with-php/#findComment-734868 Share on other sites More sharing options...
RestlessThoughts Posted January 11, 2009 Share Posted January 11, 2009 You're welcome, glad it helped. Anyway. If $ClPK is auto incrementing, where is $ClPK coming from? Is it a number being pulled from a form, or another table in the database? I get numbers from one table to the next by selecting the info from the table first and just inserting it into the next, but if you already have $ClPK it should be inserting into both tables. Quote Link to comment https://forums.phpfreaks.com/topic/140407-insert-results-from-form-with-php/#findComment-734915 Share on other sites More sharing options...
big-dog1965 Posted January 11, 2009 Author Share Posted January 11, 2009 Cl_PK is coming from table Class_Cl, when you fill out the form it inserts data into table Class_Cl the Cl_PK is auto incroment for each record. so in the Class_Alias_Cla table I need the Cl_PK from table Class_Cl to be inserts in to the Cla_Cl_PK colum of the Class_Alias_Cla table Quote Link to comment https://forums.phpfreaks.com/topic/140407-insert-results-from-form-with-php/#findComment-734921 Share on other sites More sharing options...
RestlessThoughts Posted January 11, 2009 Share Posted January 11, 2009 Hm, so you mean $ClPK is generated when a record is inserted into table Class_Cl? Then you shouldn't have $ClPK in your first query, just " " is fine and the database should generate the number. Then you have to get the number back out of table Class_Cl before inserting data into Class_Alias_Cla. There's several ways to do this I'm sure. Google 'last ID after insert' for some ideas. When I had to do something like this, I ganked the last inserted ID from one table like so: $s = mysql_query("SELECT Cl_PK FROM `Class_Cl`") or die(mysql_error()); while ($fetch = mysql_fetch_assoc($s)) { $count = mysql_num_rows($s); $sID = 1; $sID = $count+$sID; } $ClPK = $sID; Which isn't very elegant and doesn't work well if you're inserting more than one row at a time. Tis a start for you though. Quote Link to comment https://forums.phpfreaks.com/topic/140407-insert-results-from-form-with-php/#findComment-734946 Share on other sites More sharing options...
big-dog1965 Posted January 11, 2009 Author Share Posted January 11, 2009 where do I put that code in the first table code after the insert or in the 2nd table code some where Quote Link to comment https://forums.phpfreaks.com/topic/140407-insert-results-from-form-with-php/#findComment-734954 Share on other sites More sharing options...
RestlessThoughts Posted January 12, 2009 Share Posted January 12, 2009 Well if you try it like this: $query="insert into Class_Cl (Cl_Act,Cl_PK,Cl_S_Name,Cl_F_Name,Cl_Notes,Cl_R1,Cl_R2,Cl_R3,Cl_R4,Cl_R5,Cl_R6,Cl_R7,Cl_R8,Cl_R9,Cl_R10,CL_Unknown) values ('".$ClAct."','""','".$ClSName."','".$ClFName."','".$ClNotes."','".$ClR1."','".$ClR2."','".$ClR3."','".$ClR4."','".$ClR5."','".$ClR6."','".$ClR7."','".$ClR8."','".$ClR9."','".$ClR10."','".$CLUnknown."')"; mysql_query($query); $s = mysql_query("SELECT Cl_PK FROM `Class_Cl`") or die(mysql_error()); while ($fetch = mysql_fetch_assoc($s)) { $count = mysql_num_rows($s); $sID = 1; $sID = $count+$sID; } $ClPK = $sID; $query="insert into Class_Alias_Cla (Cla_Cl_PK,Cla_F_Name) values ('".$ClPK."','".$ClFName."')"; mysql_query($query); It should insert the record into Class_Cl then take $ClPK from it before sticking the information into Class_Alias_Cla, making both records have the same number in the tables. And you don't need to auto_increment Class_Alias_Cla since the records are to always match up, though you can make it the primary key if you're afriad of an accidental overwrite. If that doesn't work for you let me know. Quote Link to comment https://forums.phpfreaks.com/topic/140407-insert-results-from-form-with-php/#findComment-734960 Share on other sites More sharing options...
big-dog1965 Posted January 12, 2009 Author Share Posted January 12, 2009 Thanks again that worked I change $sID = 1; to $sID = 0; because it was off by +1 each time an entry was made Quote Link to comment https://forums.phpfreaks.com/topic/140407-insert-results-from-form-with-php/#findComment-735024 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.