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.
Yes, I have checked my type in mysqli_db, it's a 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).
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.
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);
.
Therefore the if
statement never fired.
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.