This topic is locked
[SOLVED]

 check if specific record exists

2/18/2010 4:26:42 AM
PHPRunner General questions
R
Rens1976 author

Hi everyone

This has me scratching my head. Probably because I'm a php newbie.

I have a table called 'Bed'. It has a key field called 'Bed_ID'.

I have a table called 'resident'. It is linked to table Bed via 'Bed_ID'.

Therefore one bed can have multiple residents. The current occupant is determined by a check box called 'active' which is in the resident table.

I have an add page in my project that will add a new resident to a bed. The end user clicks on bed in the menu to access resident via a child link. From their they add someone to the bed or remove someone from a bed. I need to check weather the bed is 'occupied'(active resident is assigned to that bed_ID) before a new resident can be admitted. Therefore eliminating a situation where two residents occupy the same bed. So I'm using a check if specific record exists event. I have this below. Can someone tell me what is wrong with this.
//** Check if specific record exists ****

global $conn;

$strSQLExists = "select * from Resident where Active='1' and Bed_ID ='".$value_Bed_ID."'"; //check if the bed to be added to is occupied by an active resident.

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

$data=db_fetch_array($rsExists);

if($data)

{

// don't allow the user to add record if resident exists.

} else {

//allow record to be updated

}
Thanks for your assistance. It is greatly appreciated.

Rens

Sergey Kornilov admin 2/18/2010

I think your code is correct and all you need to do is to add return statements.

global $conn;

$strSQLExists = "select * from Resident where Active='1' and Bed_ID ='".$value_Bed_ID."'"; //check if the bed to be added to is occupied by an active resident.

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

$data=db_fetch_array($rsExists);

if($data)

{

return false;

} else {

return true;

}


PS. nothing is wrong with two residents occupying the same bed as long as they are married.