This topic is locked

SQL query Before Insert Record - Import Events

8/16/2012 1:09:19 AM
PHPRunner General questions
M
macalister author

Hello.
I have the following code in Before Insert Record (import page):

$strSQLExists1 = "select idcliente, cpagas, pagaduria, nombres, from saldosuscriptor where idcliente !=".$rawvalues["cedula"];

$rsExists1 = db_query($strSQLExists1,$conn);

while($data1 = db_fetch_array($rsExists1)){
if($data1["pagaduria"] == $rawvalues["pagaduria"] && $data1["cpagas"] >= 1){


$sql1 = "insert into cartera (cedula, nombres, valor, pagaduria, ano, mes) values('".$data1["idcliente"]."', '".$data1["nombres"]."', 0, 'N/A', '".$rawvalues["ano"]."', '".$rawvalues["mes"]."')";

CustomQuery($sql1);



}



}


But i realized that the query is executed more than one time, then i will have a lot of duplicate data.

Is possible to execute a query only one time in the import page?

C
cgphp 8/16/2012

You have to review your select query. It returns more than one record as result. You can limit the number of records in the result with the LIMIT clause, but it depends on the logic of your query:

$strSQLExists1 = "select idcliente, cpagas, pagaduria, nombres, from saldosuscriptor where idcliente !=".$rawvalues["cedula"]." LIMIT 1";
M
macalister author 8/16/2012

Thanks Cristian.
But for example i want to display a message on the web (echo "hello world"; ), it apears more of one time (number of records to be imported = number of times that the message apears). Is posible to execute some action only one time in the import events, extactly in BeforeInsert event?

C
cgphp 8/17/2012

You can control the number of times the code is executed using a session var. Unset it when the import operation is finished.

M
macalister author 8/17/2012



You can control the number of times the code is executed using a session var. Unset it when the import operation is finished.


Cristian, Can you give me a code example please? im a little confused of how to use a session var and unset it to control the execution code.
Thanks again for your time and help.