开发者

PHP MySQL_return_rows = echos 1 then 0

开发者 https://www.devze.com 2023-04-08 18:07 出处:网络
I am doing an AJAX post to a PHP processing script.I am checking if MySQL returns any rows befoe proceeding with registration.The problem I\'m having - MySQL quickly echoes 1 row and then echoes 0 and

I am doing an AJAX post to a PHP processing script. I am checking if MySQL returns any rows befoe proceeding with registration. The problem I'm having - MySQL quickly echoes 1 row and then echoes 0 and proceeds with registration, even if 0 rows exist. Why am I seeing 1 row returned, and then quickly correcting itself?

$.ajax({
            type: "POST",
            data: "username="+$(this).attr('username')+"password="+$(this).attr('email')+$(this).attr('password')+$(this).attr('passconf'),
            url: "includes/register.php",
            success: function(msg)
            {   
                alert(data);
                $("#result").data($data); 
            }
        });

    });

PHP code:

    $res = mysql_query("SELECT * FROM users WHERE username = '".$username."'");
                    if (!$res) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}
                    $num = mysql_num_rows($r开发者_高级运维es);
                    echo $num;


Your event handler probably is failing to prevent the default action, causing the form to be submitted twice (once via ajax, and once via regular browser form submission). Try returning false like this:

$(login_form).click(function() {
    $.ajax({ ... });
    return false;
});

Returning false prevents the default action and stops bubbling. In case you want bubbling to still apply, you can also explicitly prevent the default like this:

$(login_form).click(function(ev) {
    ev.preventDefault();
    $.ajax({ ... });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消