mardi 29 novembre 2016

A weird case of if else statement malfunction in PHP

I've been working over this question for a few hours and really hope im not missing anything basic. I know its mostly code but all the information is in comments within the code.

I have a function which i feed in my case: $db is array with access creds $uid is null in this case $email is a valid email address $companyid is a valid specific numeric value, which is irrelevant $fname, $lname, $mname, $role - valid strings

Now the function itself:

function hire_employee($db, $uid, $email, $companyid, $fname, $lname, $mname, $role){
    try{    
        $srvr = new PDO("mysql:host=".$db['server'].";dbname=".$db['db'], $db['mysql_login'], $db['mysql_pass'], array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
        $srvr->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        if (is_numeric($uid)){ //uid is null in our case, so we skip this piece of code
            echo "<br>trace 1";//does not output
            $set=$srvr->prepare("INSERT into roles (uid, companyid, role, assigned_date) VALUES ((SELECT uid FROM users WHERE uid=:uid and active=1), :companyid, :role, CURDATE());");
            $set->bindParam(":uid", $uid);
            $set->bindParam(":companyid", $companyid);
            $set->bindParam(":role", $role);
            if ($set->execute()){
                return true;
            }
        } elseif (isset($email)) {// this is our case, we have a valid email
            echo "<br>trace 2";//does output
            $email = filter_var($email, FILTER_SANITIZE_EMAIL);
            if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
                //is a valid email address
                //first trying to add existing user (if exists)
                echo "<br>trace 2.1";//does output
                $check=$srvr->prepare("INSERT into roles (uid, companyid, role, assigned_date) VALUES ((SELECT uid FROM users WHERE email=:email and active=1), :companyid, :role, CURDATE());");
                $check->bindParam(":companyid", $companyid);
                $check->bindParam(":role", $role);
                $check->bindParam(":email", $email);
                //NOW THE WEIRD PART!!!-----------------------
                if ($check->execute()){//checking if statement executes successfully and returns true
                    echo "<br>trace 2.1.1";//does not output, a proper thing in our case
                    return true;
                } else { //nope user does not exist, we need to create a user
                    echo "<br>trace 2.1.2";//this does not output either!!!
                    //dear stackoverflow member can skip the rest of the code below
                    echo "<br>traceYO!!! $uid, $email, $companyid, $fname, $lname, $mname, $role";
                    $newuserid=register_user($db, $fname, $mname, $lname, "NULL", "NULL", "NULL", "NULL", true, $email, "NULL");
                    echo $newuserid;
                    if (!is_numeric($newuserid) AND ($newuserif > 0)) {
                        echo "<br>trace 2.1.2.1";
                        return false;
                    } else {
                        echo "<br>trace 2.1.2.2";
                        $insert=$srvr->prepare("INSERT into roles (uid, companyid, role, assigned_date) VALUES (:uid, :companyid, :role, CURDATE());");
                        $insert->bindParam(":uid", $newuserid);
                        $insert->bindParam(":companyid", $companyid);
                        $insert->bindParam(":role", $role);
                        if ($insert->execute()){
                            echo "<br>trace 2.1.2.2.1";
                            return true;
                        }
                    }
                }
            } else {
                //is not a valid email address
                return false;
            }
        } else { 
            echo "<br>trace 3";
            return false;
        }

    }
    catch(PDOException $e) {
        report_error($db, $_SESSION['uid'], $companyid, 'hire_employee', "companyid: $companyid, fname: $fname, mname: $mname, lname: $lname, email: $email, uid: $uid, role:$role", $e->getMessage());
        return false;
    }
}

Now the only output I'm getting here:

trace 2 trace 2.1

Somehow it fails to trace 2.1.1 or 2.1.2.

Any help would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire