开发者

I can't return MySQL data

here is my JS code:

$.ajax({
            url: 'login.php',
            type: 'POST',
            data: {
            loginName:  $("#loginName").val(),
            loginPass:  $("#loginPass").val()
            },
            dataType: 'json', 
            success: function(data){
                if(data.success){
                    $("#Message").fadeIn(function(){
                        $(this).html(data.message);
                    });
                }
                else
                {
                    $("#Message").fadeIn(function(){
                        $(this).html(data.message);
                    });
                }
            }
        });

and here is PHP:

<?php
$data = array();
$data['success'] = true;
$data['message'] = "Here is the message";
echo json_encode($data);
?>

If i have this its be all right, but if i edit the php file to this:

<?
    include "db.php";
    $data = array();
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        if($_POST['loginName'] == "" && $_POST['loginPass'] == "")
        {
            @$v = mysql_query("SELECT * FROM users WHERE name LIKE '$registerName'");
          开发者_如何学Python  @$p = mysql_num_rows($v);
            if ($p == 0){
                @$v = mysql_query("INSERT INTO users VALUES('','$registerName','$registerPass')");
                $data['message'] = "Byly jste úspěšně zaregistrováni.";
                $data['success'] = true;
            }
            else
            {
                $data['message'] = "Tento uživatel je tu již zaregistrován.";
                $data['success'] = false;
            }

        echo json_encode($data);
    }
?>

The ajax do nothing... its just send data but nothing to the alert... :( and i control this mysql events and its works if i use just php...


Firstly i would like to welcome back little Bobby Tables

Now down to business, look at the following line of code closely:

if($_POST['loginName'] == "" && $_POST['loginPass'] == "")

Next on the agenda, Your error suppression is incorrect, you should control your errors using error_reporting on your config file, and use an error_handler to log the errors for your application.

You should also note that your only responding with a result if certain conditions are met, I would advise you to respond regardless of what data or post method, supply an error message for every possibility, As in development stages this would make things a lot easier.

This is how i would write the code:

include "db.php"; //error_reporting(0);
$data = array();
$user_info = array_map("mysql_real_escape_string",$_POST);

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    if(!empty($user_info['loginName']) && !empty($user_info['loginPass']))
    {
        $v = mysql_query(sprintf("SELECT * FROM users WHERE name LIKE '%s'",$user_info['loginName']));

        if(mysql_num_rows($v) > 0)
        {
            //Update Here
            //Output Success here/ json_encode
            exit;
        }
    }
}
//Output Error here/ json_encode


Why are you supressing errors? It's not even being done correctly:

@$v = mysql_query("SELECT * FROM users WHERE name LIKE '$registerName'");

For one, there's no point in supressing errors on an assignment - an assignment will always succeed. Putting the @ on the other side would be useless as well. You'd have absoultely no indication if the query blew up on you. You're not checking if it failed at all. The code should be:

$result = mysql_query(...);
if ($result === FALSE) {
    die(mysql_error());
}

Have you checked if your POST data is coming through properly? As it stands now, if neither of those POST fields is set, then your ajax response block is completely bypassed and the script will output nothing.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜