This topic is locked
[SOLVED]

 INSERT SQL QUERY RETURNS LAST RECORD ONLY

3/14/2017 2:44:38 AM
PHPRunner General questions
C
chrispa author

Hello,
after i update Table1 i need to insert ALL records to table2 BUT first need to delete previous records exists in Table2 with same EnquryLink.
INSERT query below works fine and do the job :
$strSQLInsert = "insert into tblordersupplydetails (VesselDept, EnquryLink, ShortCode, Description, SupplierLink,

UnitLink, Quantity, Curr, Rate, ForeignUnit, Discount, ForeignUnitFinal, ForeignTotal, USDUnit, USDTotal, ApproveFlag)

values ('".$values['VesselDept']."','".$values['EnquryLink']."','".$values['ShortCode']."','".$values['Description']."'

,'".$values['Supplier1']."','".$values['Unit1']."','".$values['Quantity1']."'

,'".$values['Curr1']."','".$values['Rate1']."','".$values['ForeignUnit1']."'

,'".$values['Discount1']."','".$values['ForeignTotal1']."','".$values['USDUnit1']."','".$values['USDTotal1']."'

,'".$values['Project Status']."','1') " ;
CustomQuery($strSQLInsert);
However when i add delete query before the above INSERT query :
$sql4 = "DELETE FROM tblordersupplydetails WHERE EnquryLink=".$values["EnquryLink"];

CustomQuery($sql4);
records are DELETED as they should BUT the INSERT query insers only 1 record from table 1 to table 2.
Can someone assist ?
thanks in advance.

HJB 3/14/2017



Hello,
after i update Table1 i need to insert ALL records to table2 BUT first need to delete previous records exists in Table2 with same EnquryLink.
INSERT query below works fine and do the job :
$strSQLInsert = "insert into tblordersupplydetails (VesselDept, EnquryLink, ShortCode, Description, SupplierLink,

UnitLink, Quantity, Curr, Rate, ForeignUnit, Discount, ForeignUnitFinal, ForeignTotal, USDUnit, USDTotal, ApproveFlag)

values ('".$values['VesselDept']."','".$values['EnquryLink']."','".$values['ShortCode']."','".$values['Description']."'

,'".$values['Supplier1']."','".$values['Unit1']."','".$values['Quantity1']."'

,'".$values['Curr1']."','".$values['Rate1']."','".$values['ForeignUnit1']."'

,'".$values['Discount1']."','".$values['ForeignTotal1']."','".$values['USDUnit1']."','".$values['USDTotal1']."'

,'".$values['Project Status']."','1') " ;
CustomQuery($strSQLInsert);
However when i add delete query before the above INSERT query :
$sql4 = "DELETE FROM tblordersupplydetails WHERE EnquryLink=".$values["EnquryLink"];

CustomQuery($sql4);
records are DELETED as they should BUT the INSERT query insers only 1 record from table 1 to table 2.
Can someone assist ?
thanks in advance.


https://xlinesoft.com/phprunner/docs/custom_add.htm
... for inspiration purposes only.

Sergey Kornilov admin 3/14/2017

Not sure I understand the question. That INSERT SQL query is supposed to insert a single record and I'm not sure how it can insert two records.

HJB 3/14/2017



https://xlinesoft.com/phprunner/docs/custom_add.htm'>https://xlinesoft.com/phprunner/docs/custom_add.htm
... for inspiration purposes only.


Example ( ex https://xlinesoft.com/phprunner/docs/custom_add.htm'>https://xlinesoft.com/phprunner/docs/custom_add.htm )
Lets consider the situation when records are never added directly to the main table (e.g. Cars). Instead, records are added to the temporary TempCars table and then moved to the main Cars table once approved by admin. In this case the following code in CustomAdd event will do the job:

global $dal;
$tblTempCars = $dal->Table("TempCars");
$tblTempCars->Value["make"]=$values["make"];
$tblTempCars->Value["model"]=$values["model"];
$tblTempCars->Value["yearOfMake"]=$values["yearOfMake"];
$tblTempCars->Add();
return false;


You may notice that BeforeAdd event does the similar job. The main difference is that when you return false in BeforeAdd event this situation is considered as error and user will understand that something went wrong. On the other side, returning false in CustomAdd event is perfectly legitimate and application execution continues after that.
Unquote
It is at least my personal understanding that the above is DIRECT of what you want to see in place.

C
chrispa author 3/15/2017



Example ( ex https://xlinesoft.com/phprunner/docs/custom_add.htm )
Lets consider the situation when records are never added directly to the main table (e.g. Cars). Instead, records are added to the temporary TempCars table and then moved to the main Cars table once approved by admin. In this case the following code in CustomAdd event will do the job:

global $dal;
$tblTempCars = $dal->Table("TempCars");
$tblTempCars->Value["make"]=$values["make"];
$tblTempCars->Value["model"]=$values["model"];
$tblTempCars->Value["yearOfMake"]=$values["yearOfMake"];
$tblTempCars->Add();
return false;


You may notice that BeforeAdd event does the similar job. The main difference is that when you return false in BeforeAdd event this situation is considered as error and user will understand that something went wrong. On the other side, returning false in CustomAdd event is perfectly legitimate and application execution continues after that.
Unquote
It is at least my personal understanding that the above is DIRECT of what you want to see in place.


thank you - however what i want to complete is the following .
have tables tblrequisitiondetails and tblordersupplydetails
on tblrequisitiondetails i use update 'Update selected' feature so when i complete an update of varioues field i need the script above to remove records from tblordersupplydetails and insert the new updated records . I have manage half part like without delete sql above - insert working fine but if i have it like that dublicate records are being created each time i update records in tblrequisitiondetails.
so in summary insert to other table records updated working fine without DELETE sql - when i add DELETE sql to avoid dublicates then only 1 record is beign inserted rathen than 10 or 20 etc.
thanks in advance.

HJB 3/15/2017



thank you - however what i want to complete is the following .
have tables tblrequisitiondetails and tblordersupplydetails
on tblrequisitiondetails i use update 'Update selected' feature so when i complete an update of varioues field i need the script above to remove records from tblordersupplydetails and insert the new updated records . I have manage half part like without delete sql above - insert working fine but if i have it like that dublicate records are being created each time i update records in tblrequisitiondetails.
so in summary insert to other table records updated working fine without DELETE sql - when i add DELETE sql to avoid dublicates then only 1 record is beign inserted rathen than 10 or 20 etc.
thanks in advance.


Got your point, so, view the following for inspiration purposes: http://stackoverflow.com/questions/21189466/copy-and-delete-data-from-one-table
P.S. Throughout the past weeks there had been several forum issues where people want to DELETE while HIDE is the answer. Say, EASY IS FIRST is

and shall remain my biz motto. Example, you make TWO tables and hence you complicate things, ending up in coding problems. I would only make ONE

table (to get updated by whatsoever means, with finally a "done or executed or else" field) where "done or executed or else" expression would be

placed inside the FILTER area of SQL query then, HIDING (rather than to DELETE) such records where the appropriate FILTER CRITERIA is met.

In other words, what you try is to saddle a horse from the rear, getting somehow stricken by code-ropes of all kinds during the saddling process.

C
chrispa author 3/17/2017



Got your point, so, view the following for inspiration purposes: http://stackoverflow.com/questions/21189466/copy-and-delete-data-from-one-table
P.S. Throughout the past weeks there had been several forum issues where people want to DELETE while HIDE is the answer. Say, EASY IS FIRST is

and shall remain my biz motto. Example, you make TWO tables and hence you complicate things, ending up in coding problems. I would only make ONE

table (to get updated by whatsoever means, with finally a "done or executed or else" field) where "done or executed or else" expression would be

placed inside the FILTER area of SQL query then, HIDING (rather than to DELETE) such records where the appropriate FILTER CRITERIA is met.

In other words, what you try is to saddle a horse from the rear, getting somehow stricken by code-ropes of all kinds during the saddling process.


hello
eventually found the solution and i post it here may help the others
quote
if ($values['ApprovedFlag1']== '1')

{

$strSQLInsert = "insert into tblordersupplydetails (VesselDept, EnquryLink, ShortCode, Description, SupplierLink,

UnitLink, Quantity, Curr, Rate, ForeignUnit, Discount, ForeignUnitFinal, ForeignTotal, USDUnit, USDTotal, ApproveFlag,LastUpdate)

values ('".$values['VesselDept']."','".$values['EnquryLink']."','".$values['ShortCode']."','".$values['Description']."'

,'".$values['Supplier1']."','".$values['Unit1']."','".$values['Quantity1']."'

,'".$values['Curr1']."','".$values['Rate1']."','".$values['ForeignUnit1']."'

,'".$values['Discount1']."','".$values['ForeignTotal1']."','".$values['USDUnit1']."','".$values['USDTotal1']."'

,'".$values['Project Status']."','1',now()) " ;
CustomQuery($strSQLInsert);
$sql1 = "UPDATE tblrequisitionsuppliesdetails SET ApprovedFlag2 = '0' where EnquryLink=".$values["EnquryLink"];

CustomQuery($sql1);
$sql2 = "UPDATE tblrequisitionsuppliesdetails SET ApprovedFlag3 = '0' where EnquryLink=".$values["EnquryLink"];

CustomQuery($sql2);
$sql3 = "UPDATE tblrequisitionsuppliesdetails SET ApprovedFlag4 = '0' where EnquryLink=".$values["EnquryLink"];

CustomQuery($sql3);
$sql6 = "delete tblordersupplydetails

from tblordersupplydetails

inner join (

select max(LastUpdate) as LastUpdate,EnquryLink,Description

from tblordersupplydetails

where Description in (

select Description

from tblordersupplydetails

group by Description

having count(*) > 1

)

group by Description

) duplic on duplic.Description = tblordersupplydetails.Description

where tblordersupplydetails.LastUpdate < duplic.LastUpdate";
CustomQuery($sql6);
}
unquote
thanks