A
A
ALXTNTCN2022-02-27 15:31:37
PHP
ALXTNTCN, 2022-02-27 15:31:37

Why isn't username saved to mysql database?

Good afternoon, please help me figure out why, when registering, it does not save the password to the mysql database?

register.php code

</head>

<body>
    <style>
        .help-block{
            color:red;
        }
    </style>
    <div class="auth-wrapper">
        <div class="auth-content">
            <div class="auth-bg">
                <span class="r"></span>
                <span class="r s"></span>
                <span class="r s"></span>
                <span class="r"></span>
            </div>
            <div class="card">
                <div class="card-body text-center">
                    <div class="mb-4">
                        <i class="feather icon-user-plus auth-icon"></i>
                    </div>
                    <h3 class="mb-4">Регистрация</h3>
                    <form action="<?= htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                        
                        <div class="input-group mb-1" <?= (!empty($username_err)) ? 'has-error' : ''; ?>">
                            <input type="username" class="form-control" name="username"value="<?= $username; ?>" placeholder="Никнейм">
                        </div>
                        <div class="input-group mb-3" <?= (!empty($email_err)) ? 'has-error' : ''; ?>">
                            <input type="email" class="form-control" name="email"value="<?= $email; ?>" placeholder="Email">
                        </div>
                        <span class="help-block"><?= $email_err; ?></span>
                        <div class="input-group mb-4" <?= (!empty($password_err)) ? 'has-error' : ''; ?>">
                            <input type="password" class="form-control" placeholder="Пароль" name="password" placeholder="password" value="<?= $password; ?>">
                        </div>
                        <span class="help-block"><?= $password_err; ?></span>
                        <div class="input-group mb-4" <?= (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
                            <input type="password" class="form-control" name="confirm_password" placeholder="Подтверждение пароля" value="<?= $confirm_password; ?>">
                        </div>
                        <span class="help-block"><?= $confirm_password_err; ?></span>
                        <div class="form-group text-left">
                            <div class="checkbox checkbox-fill d-inline">
                                <input type="checkbox" name="checkbox-fill-1" id="checkbox-fill-1" checked="">
                                <label for="checkbox-fill-1" class="cr"> Сохранить пароль</label>
                            </div>
                        </div>
                        <div class="form-group text-left">
                            <div class="checkbox checkbox-fill d-inline">
                                <input type="checkbox" name="checkbox-fill-2" id="checkbox-fill-2">
                                <label for="checkbox-fill-2" class="cr">Подписаться на <a href="#!"> Рассылку</a>.</label>
                            </div>
                        </div>
                        <button class="btn btn-primary shadow-2 mb-4">Регистрация</button>
                        <p class="mb-0 text-muted">Уже есть аккаунт?<a href="login.php"> Войти</a></p>
                    </form>
                </div>
            </div>
        </div>
    </div>


php_register.php code
<?php
// Define variables and initialize with empty values
$email = $password = $confirm_password = "";
$email_err = $password_err = $confirm_password_err = "";
 
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST")
{
    // Validate email
    if(empty(trim($_POST["email"])))
    {
        $email_err = "Please enter a email.";
    }
        else
    {
        // Prepare a select statement
        $sql = "SELECT id FROM users WHERE email = ?";
        
        if($stmt = mysqli_prepare($conection_db, $sql))
        {
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "s", $param_email);
            
            // Set parameters
            $param_email = trim($_POST["email"]);
            
            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt))
            {
                /* store result */
                mysqli_stmt_store_result($stmt);
                
                if(mysqli_stmt_num_rows($stmt) == 1)
                {
                    $email_err = "This email is already taken.";
                }
                    else
                {
                    $email = trim($_POST["email"]);
                }
            }
                else
            {
                echo "Oops! Something went wrong. Please try again later.";
            }

            // Close statement
            mysqli_stmt_close($stmt);
        }
    }
    
    // Validate password
    if(empty(trim($_POST["password"])))
    {
        $password_err = "Please enter a password.";     
        }
            elseif
            (strlen(trim($_POST["password"])) < 6)
        {
        $password_err = "Password must have atleast 6 characters.";
    }
        else
    {
        $password = trim($_POST["password"]);
    }
    
    // Validate confirm password
    if(empty(trim($_POST["confirm_password"])))
    {
        $confirm_password_err = "Please confirm password.";     
    }
        else
    {
        $confirm_password = trim($_POST["confirm_password"]);
        if(empty($password_err) && ($password != $confirm_password))
        {
            $confirm_password_err = "Password did not match.";
        }
    }
    
    // Check input errors before inserting in database
    if(empty($email_err) && empty($password_err) && empty($confirm_password_err))
    {
        // Prepare an insert statement
        $sql = "INSERT INTO users (email, password) VALUES (?, ?)";
         
        if($stmt = mysqli_prepare($conection_db, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "ss", $param_email, $param_password);
            
            // Set parameters
            $param_email = $email;
            $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
            
            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt))
            {
                // Redirect to login page
                header("location: login.php");
            } 
                else
            {
                echo "Something went wrong. Please try again later.";
            }
            // Close statement
            mysqli_stmt_close($stmt);
        }
    }
    // Close connection
    mysqli_close($conection_db);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2022-02-27
@FanatPHP

In principle, the approach is correct, but the code is taken from some idiotic source.
Same it is necessary to paint three lines on two screens!
In general, we read here What is the error in my code and here Why can't I write to the database?
Moreover, take an example of the code from the second answer, and write the code for working with the database normally, without all this insanity "if stmt muscle preparation, if stmt suskyuli execute, samsing vent vrong!"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question