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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?