This topic is locked

BeforeDelete event delete all child raw

7/30/2012 10:47:16 AM
PHPRunner General questions
C
CC88 author

Dear all,
I'm using this statement:
$str = "select 'numfattura','fornitore' from fattura where " .$where;

$rs = db_query($str,$conn);

$data = db_fetch_array($rs);

//$data["MasterForeignKey"]- FK
$strSQLExists = "select * from righefattura where numfattura=".$data['numfattura'] ." and fornitore=".$data['fornitore'];

$rsExists = db_query($strSQLExists,$conn);

$dataExists=db_fetch_array($rsExists);

if($dataExists)

{

$strDelete = "delete from righefattura where numfattura=".$data['numfattura'] ." and fornitore=".$data['fornitore'];

db_exec($strDelete,$conn);

}

return true;
But when I delete a row from table 'fattura' it delete all raw in 'righefattura' and not only the child row identified by $data['numfattura'] and $data['fornitore'].
Any advice?

C
cgphp 7/31/2012

Try to echo the query:

$strSQLExists = "select * from righefattura where numfattura=".$data['numfattura'] ." and fornitore=".$data['fornitore'];

echo $strSQLExists;

exit();


Make sure $data['numfattura'] and $data['fornitore'] are not empty.

C
CC88 author 7/31/2012

Thank you very much.
After printing query it is something like:
select from righefattura where numfattura=numfattura and fornitore=fornitore instead of something like select from righefattura where numfattura=537 and fornitore=1
It doesn't use in the correct way the value $data['numfattura'] and $data['fornitore'] as expected.
Any advice?

C
cgphp 7/31/2012

Something is wrong in the previous query. Try to remove the single quotes from field names:

$str = "select numfattura, fornitore from fattura where " .$where;

$rs = db_query($str,$conn);

$data = db_fetch_array($rs);