Page 1 of 1

conditions check in functions

Posted: Thu Jun 03, 2004 9:54 pm
by azrinhadi
Helloo all,

I just joined the group. And I am going through the learning curve of Php.
Hope someone could help me out here.
I want to have a form that consist of 5 text inputs, and 4 buttons(referred by $action [0-3] ).
What I am trying to do is, after the user click the first button to set the user's rights, it will update or insert into database for the specific resource that the user has the permission to.
but i was checking the user_id and res_id only initially, but can i add another checking which is not to pass the user if they missed fields like:
resource_type and permission are blank, while there is no existing record in the db (flag of $rExist).

Code: Select all

function SetUserRights($user_id,$res_type,$res_id,$permission,$action){
if($action == '0' && (!empty($user_id)) && (!empty($res_id))) {
$rExist = FALSE;
$sql = "SELECT * FROM Resource WHERE user_id='$user_id' AND res_id='$res_id'";
$rsUserRights = $this->dbConn->Execute($sql);
if ($rsUserRights->RowCount() > 0) {
$rExist = TRUE;
$sqlUpdate = "UPDATE Resource SET permission='$permission' WHERE user_id='$user_id' AND res_type='$res_type' AND res_id='$res_id'";
$this->dbConn->Execute($sqlUpdate);
echo "The specified user_id permission has been updated!";
}else {
$sqlInsert = "INSERT INTO Resource (user_id, res_type, res_id, permission)";
$sqlInsert .= "VALUES('$user_id', '$res_type', '$res_id', '$permission')";
$this->dbConn->Execute($sqlInsert);
echo "The permission has been added for the specified user!";

// Here is what I am trying to do:
if ($rExist = FALSE && (($user_id == '')||($res_type == '')||($permission == ''))){
echo "There was not enough information given to update  neither to insert into the database!";
   }
 }
} else {
echo "either user_id or/and res_id input is missing!";
  }
}
Thanks, hope i make some sense and someone would help on this.

Looks like a case of bad brackets...

Posted: Fri Jun 04, 2004 12:04 am
by paul_esk
The code you pasted has brackets in the wrong places. Specifically, I believe that the first else clause should end after the statement:

<< echo "The permission has been added for the specified user!"; >>

I believe this because otherwise the following check of

<< if ($rExist = FALSE && .... >>

is rather pointless. Aside from that, I'd say to try to clean up the logic a bit. Hope this helps...