1

下面的查询并没有告诉我用户名已经存在于数据库中,即使它确实存在。

我正在尝试学习如何绑定参数等,但我认为自己在某个地方感到困惑。

<?php
    // Include config.php
    require_once("".$_SERVER['DOCUMENT_ROOT']."/admin/config.php");

    // top.inc.php
    require_once($top_inc);
?>

<!-- Meta start -->
<title></title>
<meta name="description" content="" />
<meta name="keywords" content="" />
<!-- Meta end -->

<!-- CONTENT START -->

<?php
    // sidebar.inc.php
    require_once($sidebar_inc);

    // main.inc.php
    require_once($main_inc);

    // check if form has been submitted
    if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['submit'])){

        // initialize form errors array
        $error    = array();

        // fetch form data
        $username = $_POST['username'];
        $email    = $_POST['email'];
        $password = $_POST['password'];

        // validate form data
        if(!preg_match(constant("USERNAME_REGEX"), $username)){
            $error[] = "Please enter a username. Use 3 to 15 digits and letters";
        }
        if(!preg_match(constant('PASSWORD_REGEX'), $password)){
            $error[] = "Please enter a password. Minimum of 6 characters required";
        }
        if(!empty($password) && $password == $username){
            $error[] = "Your pasword cannot be you username for security reasons";
        }
        if(empty($email)){
            $error[] = "Please enter your email address";
        }
        if(!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL)){
            $error[] = "Your email address is not valid";
        }

        // connect to database
        sql_con();

        // Get instance of statement
        $stmt = mysqli_stmt_init($connect);

        // sql statement
        $UserExists = "
                    SELECT
                        `user_login`
                    FROM
                        `users`
                    WHERE
                        `user_login` = ? ";

        // prepare sql statement for execution
        if (mysqli_stmt_prepare($stmt, $UserExists)) {

            // bind parameters [s for string]
            mysqli_stmt_bind_param($stmt, "s", $username) or die(mysqli_stmt_error());
            // execute statement
            mysqli_stmt_execute($stmt) or die(mysqli_stmt_error());
            // check if username is found
            if(mysqli_stmt_num_rows($stmt) > 0 ){
                $error[] = 'The username you have choose has already been taken';
            }
        }

        // If errors found display errors
        if(!empty($error)){
            foreach($error as $msg){
                echo "$msg <br />";
            }
    } else {
            echo 'My Query Worked!';
        }
    }
    // signup.tpl template location
    $tpl = 'inc/tpl/signup.tpl';
    // load signup form template
    PageContentTemplate($tpl);
?>

<!-- CONTENT FINISH -->

<?php
    // footer.inc.php
    require_once($footer_inc);
?>

基本上它只是回显“我的查询有效”,即使它应该说用户名已经被使用,我在表单上输入详细信息和我知道已被使用的用户名并提交表单,我知道我正在做某事可能真的很傻,但对 mysqli 和绑定参数等很陌生。即使我看过一些例子,我也不知道我哪里出错了。

老实说,我不确定这是否是程序风格的最佳方式,我不知道 PDO/OOP,我改变的主要原因是通过在查询中使用占位符等来避免 SQL 注入。

4

1 回答 1

1

我现在看到了。您之前没有调用mysqli_stmt_store_result()mysqli_stmt_num_rows()不会报告正确的值。

    // prepare sql statement for execution
    if (mysqli_stmt_prepare($stmt, $UserExists)) {

        // bind parameters [s for string]
        mysqli_stmt_bind_param($stmt, "s", $username) or die(mysqli_stmt_error());
        // execute statement
        mysqli_stmt_execute($stmt) or die(mysqli_stmt_error());

        // check if username is found
        // FIRST : store the store the result set so num_rows gets the right value
        mysqli_stmt_store_result($stmt);

        // Now this should be 1 instead of 0
        if(mysqli_stmt_num_rows($stmt) > 0 ){
            $error[] = 'The username you have choose has already been taken';
        }
    }

从文档:

返回结果集中的行数。mysqli_stmt_num_rows() 的使用取决于您是否使用 mysqli_stmt_store_result() 来缓冲语句句柄中的整个结果集。

如果使用 mysqli_stmt_store_result(),mysqli_stmt_num_rows() 可能会立即被调用。

于 2012-04-10T13:45:41.997 回答