Why i can't check my input and register my data into database?

試著忘記壹切 提交于 2019-12-25 08:18:04

问题


It does not show any error output and validate the input field. I don't know what going on here within this code. Hopefully there someone can figure out what happen. Thanks...

here the database connection

<?php

    // this will avoid mysql_connect() deprecation error.
    error_reporting( ~E_DEPRECATED & ~E_NOTICE );
    // but I strongly suggest you to use PDO or MySQLi.

    define('DBHOST', 'localhost');
    define('DBUSER', 'root');
    define('DBPASS', '');
    define('DBNAME', 'loginquestdb');

    $conn = mysqli_connect(DBHOST,DBUSER,DBPASS);
    $dbcon = mysqli_select_db($conn,DBNAME);

    if ( !$conn ) {
        die("Connection failed : " . mysqli_error($conn));
    }

    if ( !$dbcon ) {
        die("Database Connection failed : " . mysqli_error($conn));
    }

here the html code:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Coding Cage - Login & Registration System</title>
<link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css"  />
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>

<div class="container">

    <div id="login-form">
    <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" autocomplete="off">

        <div class="col-md-12">

            <div class="form-group">
                <h2 class="">Sign Up.</h2>
            </div>

            <div class="form-group">
                <hr />
            </div>

            <?php
            if ( isset($errMSG) ) {

                ?>
                <div class="form-group">
                <div class="alert alert-<?php echo ($errTyp=="success") ? "success" : $errTyp; ?>">
                <span class="glyphicon glyphicon-info-sign"></span> <?php echo $errMSG; ?>
                </div>
                </div>
                <?php
            }
            ?>

            <div class="form-group">
                <div class="input-group">
                <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
                <input type="text" name="name" class="form-control" placeholder="Enter Name" maxlength="50" value="<?php echo $name ?>" />
                </div>
                <span class="text-danger"><?php echo $nameError; ?></span>
            </div>

            <div class="form-group">
                <div class="input-group">
                <span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span></span>
                <input type="email" name="email" class="form-control" placeholder="Enter Your Email" maxlength="40" value="<?php echo $email ?>" />
                </div>
                <span class="text-danger"><?php echo $emailError; ?></span>
            </div>

            <div class="form-group">
                <div class="input-group">
                <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>
                <input type="password" name="pass" class="form-control" placeholder="Enter Password" maxlength="15" />
                </div>
                <span class="text-danger"><?php echo $passError; ?></span>
            </div>

            <div class="form-group">
                <hr />
            </div>

            <div class="form-group">
                <button type="submit" class="btn btn-block btn-primary" name="btn-signup">Sign Up</button>
            </div>

            <div class="form-group">
                <hr />
            </div>

            <div class="form-group">
                <a href="index.php">Sign in Here...</a>
            </div>

        </div>

    </form>
    </div>  

</div>

</body>
</html>
<?php ob_end_flush(); ?>

here the php code:

<?php
    ob_start();
    session_start();
    if( isset($_SESSION['user'])!="" ){
        header("Location: home.php");
    }
    include_once 'dbconnect.php';

    $error = false;

    if ( isset($_POST['btn-signup']) ) {

        // clean user inputs to prevent sql injections
        $name = trim($_POST['name']);
        $name = strip_tags($name);
        $name = htmlspecialchars($name);

        $email = trim($_POST['email']);
        $email = strip_tags($email);
        $email = htmlspecialchars($email);

        $pass = trim($_POST['pass']);
        $pass = strip_tags($pass);
        $pass = htmlspecialchars($pass);

        // basic name validation
        if (empty($name)) {
            $error = true;
            $nameError = "Please enter your full name.";
        } else if (strlen($name) < 3) {
            $error = true;
            $nameError = "Name must have at least 3 characters.";
        } else if (!preg_match("/^[a-zA-Z ]+$/",$name)) {
            $error = true;
            $nameError = "Name must contain alphabets and space.";
        }

        //basic email validation
        if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
            $error = true;
            $emailError = "Please enter valid email address.";
        } else {
            // check email exist or not
            $query = "SELECT userEmail FROM users WHERE userEmail='$email'";
            $result = mysqli_query($query);
            $count = mysqli_num_rows($result);
            if($count!=0){
                $error = true;
                $emailError = "Provided Email is already in use.";
            }
        }
        // password validation
        if (empty($pass)){
            $error = true;
            $passError = "Please enter password.";
        } else if(strlen($pass) < 6) {
            $error = true;
            $passError = "Password must have at least 6 characters.";
        }

        // password encrypt using SHA256();
        $password = hash('sha256', $pass);

        // if there's no error, continue to signup
        if( !$error ) {

            $query = "INSERT INTO users(userName,userEmail,userPass) VALUES('$name','$email','$password')";
            $res = mysqli_query($query);

            if ($res) {
                $errTyp = "success";
                $errMSG = "Successfully registered, you may login now";
                unset($name);
                unset($email);
                unset($pass);
            } else {
                $errTyp = "danger";
                $errMSG = "Something went wrong, try again later...";   
            }   

        }


    }
?>

回答1:


You'll want to add an else statement to echo your error messages if $error is true.

// if there's no error, continue to signup
if( !$error ) {
    $query = "INSERT INTO users(userName,userEmail,userPass) VALUES('$name','$email','$password')";
    $res = mysqli_query($dbcon, $query);

    if ($res) {
        $errTyp = "success";
        $errMSG = "Successfully registered, you may login now";
        unset($name);
        unset($email);
        unset($pass);
    } else {
        $errTyp = "danger";
        $errMSG = "Something went wrong, try again later...";   
    }   

} else {
    // if there's an error, display it
    echo $nameError . ' ' . $emailError . ' ' . $passError;
}

Perhaps consider using an array to store the errors so it doesn't matter which field has an error, it'll display with easier formatting. For example, in your name validation code:

// basic name validation
if (empty($name)) {
    $error = true;
    $errorMsgs[] = "Please enter your full name.";
} else if (strlen($name) < 3) {
    $error = true;
    $errorMsgs[] = "Name must have at least 3 characters.";
} else if (!preg_match("/^[a-zA-Z ]+$/",$name)) {
    $error = true;
    $errorMsgs[] = "Name must contain alphabets and space.";
}

// in your if/else statement
if(!$error){
    // code here
} else {
    foreach($errorMsgs as $e){
        echo $e . "<br />";
    }
}


来源:https://stackoverflow.com/questions/40346824/why-i-cant-check-my-input-and-register-my-data-into-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!