English 中文(简体)
向我说明已经存在的用户名称
原标题:Statement to tell me the username already exists

下面的询问没有告诉我,数据库中已经存在用户名称,尽管如此。

我正试图学习如何约束参数等,但把自己混为一谈。

<?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);
?>

Basically it just echos out My Query Worked even though it should say the username is already taken which it is, I enter details on the form and a username I know that has been taken and submit the form, I know I am doing something probably really silly but being new to mysqli and binding parameters etc. I don t know where I am going wrong even though I have looked at some examples.

坦率地说,我不相信这是以程序风格这样做的最佳方式,我不知道PDO/OOP,我的改变主要原因是在我的问询中使用地主等来避免投放。

最佳回答

我现在看到这一点。 您没有打电话到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:

收益分成数。 使用我sqli_stmt_num_rows()取决于你是否利用我sqli_stmt_store_result()来缓冲发言中的全部结果。

如果你使用我的Sqli_stmt_store_result(),可立即打电话给我的Sqli_stmt_num_rows()。

问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签