简体   繁体   English

注册表格PHP

[英]Register form PHP

I am a beginner in PHP & MySQL, I am trying to create this register form for a project I am working on currently, here is the PHP script, btw.. If there is a lot of mistakes, it is because I have just started! 我是PHP&MySQL的初学者,我正在尝试为当前正在研究的项目创建此注册表,这是PHP脚本,顺便说一句。如果有很多错误,那是因为我刚刚开始!

The problem I am having is that it does not check the database if the email or username match, because if it did, it should reject. 我遇到的问题是,它不会检查电子邮件或用户名是否匹配的数据库,因为如果匹配,则应该拒绝。

PHP code: PHP代码:

    <?php

//register php

error_reporting(0);

$regUsername = $_POST['reg-username'];
$regEmail = $_POST['reg-email'];
$regPassword = $_POST['reg-password'];

if(isset($regUsername) && isset($regEmail) && isset($regPassword)){
        $connect = mysql_connect('localhost', 'root', '');
        $selectDB = mysql_select_db('supermazad');
        $query = mysql_query("INSERT INTO users (username, email, password) VALUES ('$regUsername', '$regEmail', '$regPassword')");
        $checkIfSame = mysql_query("SELECT * FROM users WHERE username AND email LIKE '$query' ");
        echo '<h1 class="successMessage">You have successfully registered!</h1>';
}
            if($regUsername || $regEmail == $checkIfSame){
            echo '<h1 class="errorMessage">The username or e-mail already exists.</h1>';
        }

?>

You are trying to check if the username and email already exist in the database after inserting the username and email into the database. 将用户名和电子邮件插入数据库后,您正在尝试检查数据库中是否已存在用户名和电子邮件。 You should check beforehand either via a select with the username and email provided in the registraton, or by placing a unique key constraint on username and email fields. 您应该通过选择使用注册中提供的用户名和电子邮件的方式进行预先检查,或者通过对用户名和电子邮件字段设置唯一的键约束来进行检查。

  1. Please use mysqli instead of mysql because it's safer 请使用mysqli代替mysqli,因为它更安全
  2. remove error_reporting(0) 删除error_reporting(0)
  3. don't use "like" because it might have many result 不要使用“赞”,因为它可能会产生很多结果
  4. syntax error on the mysql query mysql查询语法错误

try this one just debug if there are error but I think this works 试试这个只是调试,如果有错误,但我认为这可行

    $regUsername    = $_POST['reg-username'];
$regEmail       = $_POST['reg-email'];
$regPassword    = $_POST['reg-password'];

if(isset($regUsername) && isset($regEmail) && isset($regPassword)){
        $mysqli         = mysqli_connect("localhost","root","","supermazad") or die("Error " . mysqli_error($mysqli)); 
        $queryInsert    = "INSERT INTO users (username, email, password) 
                            VALUES 
                          ('$regUsername', '$regEmail', '$regPassword')"; 
        mysqli_query($mysqli, $queryInsert); //this will insert the data from db

        $queryUser  = "SELECT username,email FROM users WHERE username = '$regUsername' AND email = '$regEmail'"; // this will authenticate and uses an exact match
        $result     = $mysqli->query($queryUser);
        $row        = mysqli_fetch_array($result);

        echo '<h1 class="successMessage">You have successfully registered!</h1>';
}
            if($regUsername == $row['username'] && $regEmail == $row['email']){ //this check if email and username are match with database $row['username'] store username from mysql field and $row['email'] do the same
            echo '<h1 class="errorMessage">The username or e-mail already exists.</h1>';
        }

by the way this is normal for a beginner but please improve your coding in the near future, make it cleaner and organize 顺便说一句,这对于初学者来说是很正常的,但是请在不久的将来改进您的编码,使其更加整洁并井井有条

I think you should do it like this after $selectDB = mysql_select_db('supermazad'); 我认为您应该在$selectDB = mysql_select_db('supermazad');之后这样做$selectDB = mysql_select_db('supermazad'); :

$checkIfSame = mysql_query("SELECT * FROM users WHERE username LIKE '".$regUsername."' OR email LIKE '".$regEmail."' ");
if (mysql_num_rows($checkIfSame ) > 0) {
    echo '<h1 class="errorMessage">The username or e-mail already exists.</h1>';
} else {
    //insert new user here
    $query = mysql_query("INSERT INTO users (username, email, password) VALUES ('$regUsername', '$regEmail', '$regPassword')");
}

Also, you have this in your post : 另外,您在帖子中有此内容:

if the email or username match 如果电子邮件或用户名匹配

so you should be using OR , not AND 因此,您应该使用OR而不是AND

Try this: 尝试这个:

$checkIfSame = mysql_query("SELECT * FROM users WHERE username LIKE '".$regUsername."' OR email LIKE '".$regEmail."' ");
if (mysql_num_rows($checkIfSame ) > 0) {
    echo '<h1 class="errorMessage">The username or e-mail already exists.</h1>';
} else {
    //insert new user here
    $query = mysql_query("INSERT INTO users (username, email, password) VALUES ('$regUsername', '$regEmail', '$regPassword')");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM