duaiwu8385 2016-12-30 08:53
浏览 43
已采纳

通用错误消息与注册表单一起使用,但需要特定的消息来表示重复的名称

I created a registration form using PHP, html, and Bootstrap. This code works on a basic level, and if a duplicate username is entered it shows a generic error message since 'username' and 'email' are unique in my database. The problem with that is I want the user to understand what went wrong, so they don't repeatedly try to enter the same username or email over and over again. I didn't think about it until I finished the page. Doh!

This is the working php script for the generic message:

//FROM REGISTER.PHP
<?php

    require_once('connect.php'); //<--MY DATABASE FILE
    if(isset($_POST) & !empty($_POST)) {
        $username = mysqli_real_escape_string($connection, $_POST['username']);
        $email = mysqli_real_escape_string($connection, $_POST['email']);
        $password = sha1($_POST['password']);
        $sql = "INSERT INTO `users` (username, email, password) VALUES ('$username', '$email', '$password')";

        $result = mysqli_query($connection, $sql);

    if($result) {
        $successMessage = "User Registration Successful! Please Login.";
    }
    else { //GENERIC MESSAGE FOR ALL ERRORS, INCLUDING DUPLICATES
        $failMessage = "Something went wrong. User Registration failed.";
    }
  }
?>

Here is my snippet of html, php, and Bootstrap:

<div class="container">
    <!-- HERE ARE THE ALERT DIVS THAT POP UP -->
    <?php if(isset($successMessage)){ ?><div class="alert alert-success text-center" role="alert"><?php echo $successMessage; ?> </div><?php } ?>
    <?php if(isset($failMessage)){ ?><div class="alert alert-danger text-center" role="alert"><strong>Error: </strong> <?php echo $failMessage; ?> </div><?php } ?>
    <form class="form-signin" method="POST">

The alerts pop up directly above the form, as seen in these snapshots:

Error Alert

Success Alert

I have tried so many things before coming here to no avail. For example, I tried:

$duplicate = mysqli_query("SELECT username FROM users WHERE username='".$_POST['username']."'");
//ALSO TRIED
//$duplicate = mysqli_query("SELECT username FROM users WHERE username = 'username');

if(mysqli_num_rows($duplicate) > 0) {
    $failMessage = "Username already exists";
}

However, it completely skips over that and gives me the generic Error message. I've seen so many examples/answers on how to prevent duplicates, none of which are helping me figure this out.

I've read about creating separate indexes for username and email, but I would have to do more reading on databases to understand what that entails.

  • 写回答

3条回答 默认 最新

  • doufuhao0566 2016-12-30 08:59
    关注

    Use the below code:

    Require to add $connection->errno == 1062

    <?php
    
            require_once('connect.php'); //<--MY DATABASE FILE
            if(isset($_POST) & !empty($_POST)) {
                $username = mysqli_real_escape_string($connection, $_POST['username']);
                $email = mysqli_real_escape_string($connection, $_POST['email']);
                $password = sha1($_POST['password']);
                $sql = "INSERT INTO `users` (username, email, password) VALUES ('$username', '$email', '$password')";
    
                $result = mysqli_query($connection, $sql);
        if($result) {
                $successMessage = "User Registration Successful! Please Login.";
        }else{
    
        if ($connection->errno == 1062) {
              $failMessage = "User already exists";
        }else{
        $failMessage = "Something went wrong. User Registration failed.";
        }
    
        }
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀