This topic is locked

Prevent Delete Parent Record If has Child Record

11/9/2008 3:39:13 PM
PHPRunner General questions
E
erago author

I wish to Prevent/ Restrict the ability to delete the parent record if there are any child records from other users.
I am using the following code with no Error message. But it still delete's the Record, yet diplays the message, indicating that it is recognizing the correct fields.

why is it still deleting the record?
code:

Event/List page/ before Record deleted,
**
// Parameters:

// $deleted_values - Array object.

// Each field on the List page is represented as a 'Field name'-'Field value' pair

// $where - string with WHERE clause pointing to record to be deleted.
//** Prevent Delete if child record exists ****

global $conn;

$strSQLExists = "select count(*) from _Product_Details where Product_Number='".$deleted_values["Product_Number"]."'";

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

$data=db_fetch_array($rsExists);

if ($data)

{

// if record exists do something

echo "<font color=red> Other Users have Items attached to this Product, it cannot be deleted .</font>";

}
else

{

// if dont exist do something else

}
return true;
// return true if you like to proceed with deleting record

// return false otherwise
*****

Anyone know what I am missing?

J
Jane 11/10/2008

Hi,
here is the correct code:

...

if ($data)

{

// if record exists do something

echo "<font color=red> Other Users have Items attached to this Product, it cannot be deleted .</font>";

return false;

}

else

{

// if dont exist do something else

return true;

}

E
erago author 11/10/2008

after trying the " return false; " I have found,
That it does prevent the user from deleting the record. but after running some test's, I found that it isn't basing the delete function restriction off of any condition. it is just now restricting the ability to delete any record in that table regardless of whether it has a child record.
what else am I missing ?

**
// Parameters:

// $deleted_values - Array object.

// Each field on the List page is represented as a 'Field name'-'Field value' pair

// $where - string with WHERE clause pointing to record to be deleted.
//** Prevent Delete if child record exists ****

global $conn;

$strSQLExists = "select count(*) from _Product_Details where Product_Number='".$deleted_values["Product_Number"]."'";

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

$data=db_fetch_array($rsExists);

if ($data)

{

// if record exists do something

echo "<font color=red> Other Users have Items attached to this Product, it cannot be deleted .</font>";

return false;

}
else

{

// if dont exist do something else

}
return true;
// return true if you like to proceed with deleting record

// return false otherwise


J
Jane 11/11/2008

Hi,
It's difficult to tell you what's happening without seeing actual files.

Try to print SQL query before and check $data array:

global $conn;

$strSQLExists = "select count(*) from _Product_Details where Product_Number='".$deleted_values["Product_Number"]."'";

echo $strSQLExists;

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

$data=db_fetch_array($rsExists);

print_r($data);

...

E
erago author 11/12/2008

Thanks that is a great trick.
Now I have this code and it works, for what I was trying to do. but now I get an ;

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

when user clicks on add new record.
Here is the sort of working code
****

// Parameters:

// $deleted_values - Array object.

// Each field on the List page is represented as a 'Field name'-'Field value' pair

// $where - string with WHERE clause pointing to record to be deleted.
//** Check if specific record exists ****

global $conn;

$limit=1; // number of items

$strSQLExists = "select count(*) from _Product_Details where Product_Number='".$deleted_values["Product_Number"]."'";

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

$data=dbfetchnumarray($rsExists);

if ($data[0]>=$limit)
// if record exists do something

echo "<font color=red>A Product with Items Cannot be deleted.</font>";
else

// if dont exist do something else
return true;

// return true if you like to proceed with deleting record

// return false otherwise


J
Jane 11/13/2008

It's difficult to tell you what's happening without seeing actual files.
Please publish your project on Demo Account and send to support@xlinesoft.com a URL to your pages along with instructions on reproducing this error.

I'll find what's wrong with your project inspecting it at Demo account site.