Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
658 views
in Technique[技术] by (71.8m points)

php - Why doesn't it want to register me

for a school assingment i have to make a portfolio. this has to contain a working login and registration system. I got it where it will log people in but only if i manually input data in a database. but now when i try to let people register them self it just keeps giving me the error that the username and email allready exists, what is not true in most cases. i hope u guys can help me with this. here is the code:

<?php
include_once 'db_connect.php';
include_once 'psl-config.php';

$error_msg = "";

if (isset($_POST['username'], $_POST['email'], $_POST['p'])) {
    $username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
    $email = filter_var($email, FILTER_VALIDATE_EMAIL);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error_msg .= '<p class="error">The email address you entered is not valid!</p>';
    }
    $password = filter_input(INPUT_POST, 'p', FILTER_SANITIZE_STRING);
    if (strlen($password) != 128) {
        $error_msg .= '<p class="error">Invalid password configuration.</p>';
    }
    $query_username = "SELECT id
    FROM members
    Where username == '$username'
    LIMIT 1";
    $available_username = array();
    if ($resultUsername = mysqli_query($mysqli, $query_username)) {
        if (mysqli_num_rows($resultUsername) > 0) {
            $error_msg .= '<p class="error">A user with this username already exists!</p>';
        }
    }
    $query_email = "SELECT id
    FROM members
    Where email == '$email'
    LIMIT 1";
    $available_email = array();
    if ($resultEmail = mysqli_query($mysqli, $query_email)) {
        if (mysqli_num_rows($resultEmail) > 0) {
            $error_msg .= '<p class="error">A user with this username already exists!</p>';
        }
    }
    if (empty($error_msg)) {
        $ipadress = $_SERVER['REMOTE_ADDR'];
        $random_salt = hash('sha512', uniqid(openssl_random_pseudo_bytes(16), TRUE));
        $password = hash('sha512', $password . $random_salt);
        if (!$tableRowEmail = 1) {
            $sqlinsert = "INSERT INTO members (username, email, ipadress, password, salt) VALUES ($username, $email, $ipadress, $password, $random_salt)";
            if (!mysqli_query($mysqli, $sqlinsert)) {
                header('Location: ../error.php?err=Registration failure: INSERT');
            }
        }
        header('Location: ./register_success.php');
    }
}
?>

thanks everyone, made the changes. but now it lets everything through. and it doesn't register anything in my localhost/phpmyadmin. any thoughts?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are using a single equals instead of a double for that line:

$tableRowUsername == 1

Additionally you should count the number of rows in the SQL result instead of just checking that a row is returned by checking that it is equal to 1.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...