This topic is locked
[SOLVED]

 Insert data from two tables to another

7/28/2014 2:05:01 AM
PHPRunner General questions
F
fsgonzales author

Dear Sir,
I have selection on the list page and wanted to insert on tblmandatecourse, I wanted include the Masterkey as StudentID as one of the field to be filled on my tblmandatecourse. Kindly requesting help on how that I may be able to achieved that sequence. StudentID will be coming from the master table.
global $dal;

for ($i=0; $i<count($keys); $i++)

{

// copy records from table1 to table2

$sql = "insert into tblmandatecourse (crewrank, StudentID, CourseID, status, ClassName, ClassStandard, Instructor, TotalItems) select crewrank, ID, status, ClassName, ClassStandard, Instructor, TotalItems from tblcourselist where ID=". $keys[$i]['ID'];

CustomQuery($sql);

}
$result["txt"] = "Records were copied.";

Sergey Kornilov admin 7/28/2014

Try something like this:

global $dal;

for ($i=0; $i<count($keys); $i++)

{

// copy records from table1 to table2

$sql = "insert into tblmandatecourse (crewrank, StudentID, CourseID, status, ClassName, ClassStandard, Instructor, TotalItems) select crewrank, ".$_SESSION["tblcourselist_masterkey1"].", status, ClassName, ClassStandard, Instructor, TotalItems from tblcourselist where ID=". $keys[$i]['ID'];

CustomQuery($sql);

}
$result["txt"] = "Records were copied.";
F
fsgonzales author 7/29/2014



Try something like this:

global $dal;

for ($i=0; $i<count($keys); $i++)

{

// copy records from table1 to table2

$sql = "insert into tblmandatecourse (crewrank, StudentID, CourseID, status, ClassName, ClassStandard, Instructor, TotalItems) select crewrank, ".$_SESSION["tblcourselist_masterkey1"].", status, ClassName, ClassStandard, Instructor, TotalItems from tblcourselist where ID=". $keys[$i]['ID'];

CustomQuery($sql);

}
$result["txt"] = "Records were copied.";



Sir,
Already apply your suggestion, no luck on application. Will it be possible if I just call on the Master field directly, by putting code snippet or something. Thanks for the reply

G
gudon 7/29/2014

Looks like you have a custom button on the master table list page and you are working on the server side code.
First, you have listed 8 fields that need to insert into the detail table, whereas from the master table, you selected only 7 fields. You may consider removing CourseID from the list.
Second, in your detail table, please double check whether StudentID is the key field (using phpMyAdmin if you have MySQL database). If yes, please don't make it auto-increment. Better not to make it key field at all. And make sure the CourseID field could be null.
Third, for the button coding, please make sure your client side (before and after) codes have no errors. Then try this one and see whether it works.

global $dal;

for ($i=0; $i<count($keys); $i++)

{

// copy records from table1 to table2

$sql = "insert into tblmandatecourse (crewrank, StudentID, status, ClassName, ClassStandard, Instructor, TotalItems) select crewrank, ID, status, ClassName, ClassStandard, Instructor, TotalItems from tblcourselist where ID=".$keys[$i]["ID"];

CustomQuery($sql);

}
$result["txt"] = "Records were copied.";


Good luck.