[英]Check to see whether loginName already exists or not in Database
What I want to achieve: a check to see whether the "name" already exists in one of my rows. 我要实现的目标:检查我的某一行中是否已经存在“名称”。
Problem: It keeps on adding rows even though they already exist. 问题:即使行已经存在,它也会继续添加行。
I have been looking around on StackOverflow, but haven't really found something that could possibly be causing the same problem. 我一直在寻找StackOverflow,但还没有真正发现可能导致相同问题的东西。
Yes, I have checked my type in mysqli_db, it's a VARCHAR (255)
. 是的,我已经在mysqli_db中检查了我的类型,它是
VARCHAR (255)
。 So the type I am passing on is a "string" (I am using prepared statements). 因此,我要传递的类型是“字符串”(我正在使用准备好的语句)。
I have already checked the variable $loginName
, this one is also correct (as in the right variable im passing on). 我已经检查了变量
$loginName
,这也是正确的(如正确的变量im继续)。
I have also checked whether I made a typo in my $sqli
statement relating to the table or row (loginName), but the sqli statement is correct. 我还检查了我是否在与表或行(loginName)有关的
$sqli
语句中输入了错字,但sqli语句正确。
How my code looks like: 我的代码如下所示:
include_once 'dbConn.php';
$loginName = $_POST['loginName'];
$userName = $_POST['userName'];
$pwd = $_POST['pwd'];
$confirmPWD = $_POST['confirmPWD'];
$sqli = "SELECT * FROM registerandlogin WHERE loginName = ?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sqli)) {
header('Location: index.php?prep=failed');
exit();
} else {
mysqli_stmt_bind_param($stmt, 's', $loginName);
$result = mysqli_stmt_get_result($stmt);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
header('Location: index.php?loginName=taken');
exit();
} else {
//do something else
}
I forgot the following piece of code: mysqli_stmt_execute($stmt);
我忘记了以下代码:
mysqli_stmt_execute($stmt);
. 。
Therefore the if
statement never fired. 因此,
if
语句永远不会触发。
Wrong: 错误:
mysqli_stmt_bind_param($stmt, 's', $loginName);
$result = mysqli_stmt_get_result($stmt);
$resultCheck = mysqli_num_rows($result);
Right: 对:
mysqli_stmt_bind_param($stmt, 's', $loginName);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$resultCheck = mysqli_num_rows($result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.