开发者

PHP MySQL_return_rows = echos 1 then 0

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜