MySQLi声明不起作用?

时间:2012-04-10 12:46:07

标签: mysqli prepared-statement

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

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

    // 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注入。

谢谢!

1 个答案:

答案 0 :(得分: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';
        }
    }

From the docs:

  

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

     

如果你使用mysqli_stmt_store_result(),可以立即调用mysqli_stmt_num_rows()。