Jump to content

Recommended Posts

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);

Link to comment
https://forums.phpfreaks.com/topic/140407-insert-results-from-form-with-php/
Share on other sites

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);

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.

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

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.