当前位置: 首页 > 知识库问答 >
问题:

致命错误:使用PDO[duplicate]中的临时表对布尔值调用成员函数bind_param()

孙京
2023-03-14

所以我有2个表:管理员和雇员只有2列他们共同分享

一份登入表格

和2个独立的仪表板

为了将它们临时放在一个表中,我将它们与UNION合并,并添加了一个临时列来定义用户的类型,从而登录到正确的仪表板。

因此,在经过长时间的搜索和大量的跟踪和错误之后,我得出结论,这是我作为一个新手在这个领域所能做和理解的最好的事情。

这就是我最后一篇文章的样子:

    // preparing a temporary table that unions the admin and employee tables
$TMP = "SELECT EMP_ID AS ID, EMP_EMAIL AS EMAIL, 2 AS TYPE FROM employee
                        UNION
                        SELECT ID, EMAIL, 1 AS TYPE FROM admin ";
$result = $con->prepare($TMP);                      
$result-> execute();
$result-> store_result();
$result-> fetch();

    // preparing select statement for logging in
$stmt = $con->prepare('SELECT `TYPE` FROM `".$result."` WHERE `ID` = ?');
    // Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use "s"
    $stmt->bind_param('i', $_POST['ID']);
    $stmt->execute();
    // Store the result so we can check if the account exists in the database.
    $stmt->store_result();
    if ($stmt->num_rows > 0) {
    $stmt->bind_result($ID);
    $stmt->fetch();
    }
        if ($stmt['TYPE'] == 1) {
            if ($_POST['ID'] == $ID) {
        // Verification success! User has loggedin!
        // Create sessions so we know the user is logged in, they basically act like cookies but remember the data on the server.
            session_regenerate_id();
            $_SESSION['loggedin'] = TRUE;
            $_SESSION['email'] = $_POST['EMAIL'];
            $_SESSION['id'] = $ID;
            echo 'Welcome ' . $_SESSION['email'] . '!';
        } else {
        echo 'Incorrect password!';
    }
        } else if ($stmt['TYPE'] == 2) {
    if ($_POST['ID'] == $ID) {
        // Verification success! User has loggedin!
        // Create sessions so we know the user is logged in, they basically act like cookies but remember the data on the server.
        session_regenerate_id();
        $_SESSION['loggedin'] = TRUE;
        $_SESSION['email'] = $_POST['EMAIL'];
        $_SESSION['id'] = $ID;
        echo 'Welcome ' . $_SESSION['email'] . '!';
    } else {
        echo 'Incorrect password!';
    }
        }
    $stmt-> close();



$result-> close();
$con-> close();

使用的代码来自一个用于记录一个用户的教程,它对我来说很好,但由于我的目标不同,我尝试根据需要修改它,但不幸的是,绑定参数的致命错误不断出现。

我真的很感谢你给我一些时间。非常感谢。

共有1个答案

苗承
2023-03-14

问题出在外部引号vs

当准备好的语句失败时,它将返回一个布尔值(false)。

文献资料

mysqli_prepare()返回语句对象,如果发生错误,则返回FALSE。

我想你的意思是这样写的:

$stmt = $con->prepare("SELECT `TYPE` FROM `".$result."` WHERE `ID` = ?");
 类似资料: