This topic is locked
[SOLVED]

 Insert into 1 of 2 tables

11/14/2016 3:25:25 PM
PHPRunner General questions
mtpocket author

Hi,
I have 3 tables namely;

General_tbl

  • ID = pri = ai
  • Warranty = varchar =Y/N
  • Name = varchar
  • Addr = text
  • Device = varchar
  • SN = varchar
    Warranty_tbl
  • ID = pri
  • Device
  • SN
    Non_Warranty_tbl
  • ID = pro
  • Device
  • SN
    Basically what I'm trying to achieve is when users capture or Insert to the general_tbl it'll automatically insert to either the Warranty_tbl or Non_Warranty_tbl based on the field selected in General_tbl. I cannot seem to find this in the forum as you cannot use the where clause when inserting into another table.
    Thanks

    <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=question&id=24293&image=1&table=forumtopics' class='bbc_emoticon' alt=':unsure:' />

lefty 11/16/2016



Hi,
I have 3 tables namely;

General_tbl

  • ID = pri = ai
  • Warranty = varchar =Y/N
  • Name = varchar
  • Addr = text
  • Device = varchar
  • SN = varchar
    Warranty_tbl
  • ID = pri
  • Device
  • SN
    Non_Warranty_tbl
  • ID = pro
  • Device
  • SN
    Basically what I'm trying to achieve is when users capture or Insert to the general_tbl it'll automatically insert to either the Warranty_tbl or Non_Warranty_tbl based on the field selected in General_tbl. I cannot seem to find this in the forum as you cannot use the where clause when inserting into another table.
    Thanks

    <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=80696&image=1&table=forumreplies' class='bbc_emoticon' alt=':unsure:' />


Based on what field in general table? Assuming your using Device field, Use after add event in Genral_tbl . If not change fields below to the field you want to add. If your not sure of query see Add into another table docs
You can use if statement in after add event
IF ($values["Device"] == 'somevalue') {
// Insert into table warranty
global $dal;

$tblWarranty_tbl = $dal->Table("Warranty_tbl");

$tblWarranty_tbl->Param["Matchingvalue"]=$values["Matchingvalue"];

$tblWarranty_tbl->Value["Device"]=$values["Device"];

$tblWarranty_tbl->Add();
} else {
IF ($values["Device"] == 'someothervalue') {
//Insert into table Non_Warranty
global $dal;

$tblNon_Warranty_tbl = $dal->Table("Non_Warranty_tbl");

$tblNon_Warranty_tbl->Param["Matchingvalue"]=$values["Matchingvalue"];

$tblNon_Warranty_tbl->Value["Device"]=$values["Device"];

$tblNon_Warranty_tbl->Add();
}
// all $values are case sensitive or it will not work . Also text fields are quoted / numeric don't have to be .
Just noticed you need to add a parameter to this for where . Code Updated above