所以我有 2 个表: admin和employee ,只有 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();
所使用的代码来自用于记录一个用户的教程,并且对我来说运行良好 但是由于我的目标不同,因此我尝试根据自己的需要对其进行修改,但不幸的是,关于绑定参数的致命错误不断弹出。
我非常感谢您抽出一些时间。 谢谢。
答案 0 :(得分:2)
问题出在外引号'
与"
当准备好的语句失败时,它将返回一个布尔值(false)。
mysqli_prepare()返回一个语句对象,如果发生错误则返回FALSE。
我认为您的意思是这样写:
$stmt = $con->prepare("SELECT `TYPE` FROM `".$result."` WHERE `ID` = ?");