dongxu3029 2018-12-04 19:33
浏览 36
已采纳

检查数据库中是否已存在loginName

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  
}
  • 写回答

1条回答 默认 最新

  • douxuanjie2692 2018-12-05 14:18
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大