PHP中if else语句故障的一个奇怪的例子

时间:2016-11-29 18:13:50

标签: php if-statement pdo

我已经在这个问题上工作了几个小时,真的希望我不会错过任何基本的东西。我知道它主要是代码,但所有信息都在代码中的注释中。

我有一个功能,我在我的情况下提供: $db是具有访问信誉的数组 在这种情况下,$uid为空 $email是有效的电子邮件地址 $companyid是有效的特定数值,与此无关 $fname, $lname, $mname, $role - 有效字符串

现在功能本身:

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;
    }
}

现在我得到的唯一输出是:

追踪2 跟踪2.1

不知何故,它无法追踪2.1.1或2.1.2。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:-1)

我明白了。该行if ($check->execute())生成了错误异常,立即得到了CAUHGT,从而阻止了函数的进一步执行。我实际上看到错误被添加到我决定我想要它们的地方,但是,作为一个例外的菜鸟,我没有把两个和两个放在一起。我将ATTR_ERRMODE更改为PDO::ERRMODE_WARNING,似乎工作正常。我收到警告,execute()没有返回true,功能按预期进行。

如果有人告诉我如何捕捉警告,我将非常感激。

无论如何,谢谢大家!

UPD_1:

允许生成任何警告是一种非常糟糕的做法,我正在相应地调整函数和/或语句。