如果用户名已存在PHP v5,请检查数据库

时间:2016-02-15 18:26:42

标签: php mysql

似乎无法解决这个问题,不知道是不是因为我使用的是v5 +但是“ mysqli_num_rows($ result)> 0 ”无效,因为它假设是......但它仍然将变量注入数据库。

我收到的错误是“ 警告:mysqli_num_rows()要求参数1为mysqli_result,第22行的C:\ xampp \ htdocs \ lichie \ register.php中给出布尔值

我在stackoverflow上看到的其他示例使用“ mysql_query ”方法,但我尝试使用“ mysqli_query ”。

以下是代码:

<?php

include "connect_db_users.php";

@$username = strtolower($_POST['user']);
@$email = strtolower($_POST['email']);
@$pass = $_POST['pass'];
@$confirmPass = $_POST['confirmPass'];

$ins_user = "INSERT INTO lichie_user(user_user,user_pass,user_email, user_date)"
            . "VALUES ('" . $username . "' , '" . $pass . "' , '" . $email . "' , NOW())";

if(isset($_POST['submit'])){
    if($username == !''){
        if($email == !''){
            if($pass == !''){
                if($pass == $confirmPass){

                    $query = 'SELECT user_user FROM lichie_user WHERE user_user=$username';
                    $result = mysqli_query($connect,$query);

                    if (mysqli_num_rows($result) > 0){
                        echo "Username already exists!";
                    }else{
                        if(mysqli_query($connect, $ins_user)){
                            mysqli_close($connect);
                            /*header("Location: Success.php");*/
                            echo "INSERTED";
                        }else{
                            echo "FAILED!";
                        }
                    }

                }else{
                    echo "Passwords do not match!";
                }
            }else{
                echo "Password can not be empty!";
            }
        }else{
            echo "Email can not be empty!";
        }
    }else{
        echo "Username can not be empty!";
    }
}




?>




<form method="POST">

    <h2 align="center"> Register</h2>

    <table id="table1"; cellspacing="5px" cellpadding="5%"; align="center">

            <tr>
                <td align="right">Username:</td>
                <td><input type="text" name="user" /></td>
            </tr>

            <tr>
                <td valign="top" align="right">Email:</td>
                <td><input type="email" name="email" /></td>
            </tr>

            <tr>
                <td valign="top" align="right">Password:</td>
                <td><input type="password" name="pass" autocomplete="off" /></td>
            </tr>

            <tr>
                <td valign="top" align="right">Confirm Password:</td>
                <td><input type="password" name="confirmPass" autocomplete="off"/></td>
            </tr>

            <tr>
                <td><input type="submit" name="submit" value="submit"/></td> 
            </tr>

    </table> 
</form>

1 个答案:

答案 0 :(得分:1)

您的选择查询错误。您在'附近遗失$username

试试这个:

$query = "SELECT user_user FROM lichie_user WHERE user_user='$username'";

另请参阅:How can I prevent SQL injection in PHP?