JQuery Ajax没有与MySQL交互

时间:2015-04-19 17:26:50

标签: javascript php jquery ajax mysqli

我正在尝试使用php,ajax和jquery创建一个非常基本的浏览器MMORPG。但是,网页似乎没有正确地与MySQL服务器交互。

这是我的php类,它定义了一些SQL函数:

<?php
class Db {
protected static $connection;

public function connect() {
    if(!isset(self::$connection)) {
        $config = parse_ini_file('config.ini'); 
        self::$connection = new mysqli('localhost',$config['username'],$config['password'],$config['dbname']);
    }
    if(self::$connection === false) {
        return false;
    }
    return self::$connection;
}

public function query($query) {
    $connection = $this -> connect();
    $result = $connection -> query($query);
    return $result;
}

public function select($query) {
    $rows = array();
    $result = $this -> query($query);
    if($result === false) {
        return false;
    }
    while ($row = $result -> fetch_assoc()) {
        $rows[] = $row;
    }
    return $rows;
}

public function error() {
    $connection = $this -> connect();
    return $connection -> error;
}

public function quote($value) {
    $connection = $this -> connect();
    return "'" . $connection -> real_escape_string($value) . "'";
}
public function close()
{
    mysqli_close(self::$connection);
}
}
?>

与config.ini一起引用:

[database]
username = root
password = ***
dbname = ***

(密码和数据库名称已被省略,但我已检查它们是否正确。)

以下是我使用的validation.php文件:

<?php
include 'DbClass.php';
$db = new Db();
$name= = $db -> quote($_POST['user_name']);
$pass=md5($db -> quote($_POST['password']));
$rows = $db -> select("select * from players where name=" . $name . " and password=" . $pass);
if($rows)
{
   echo json_encode("1");
}
else
{
   echo json_encode("0");
}

?>

这是登录javascript文件,其中包含在索引页面上调用的函数。

function login()
{
        $("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);  
        $.ajax({
            url : 'scripts/php/validation.php',
            type : 'POST',
            data : { user_name:$('#username').val(),password:$('#password').val()
        },
        dataType:'json',
        success : function(data) { 
          if(data == 1)
                {
                        $("#msgbox").fadeTo(200,0.1,function()
                        { 
                          $(this).html('Logging in...').addClass('alert alert-success').fadeTo(900,1,
                          function()
                          { 
                              buildPage();
                          });

                        });
            }
            else
            {
               $("#msgbox").fadeTo(200,0.1,function()
                    { 
                      $(this).html('Your login details are incorrect').addClass('alert alert-danger').fadeTo(900,1);
                    });      
            }
        }
    });
}

function registration()
{
      document.getElementById("registerBox").innerHTML = "<form class='form-horizontal' method='post' action='' id='register_form'>" +
                      "<div class='row'><div class='col-md-6 column'><div class='control-group'>" + 
                        "<label class='control-label' for='username'>Username</label><div class=''controls'>" + 
                          "<input type='text' id='registerUsername' placeholder='Username'>" +
                        "</div></div>" +
                      "<div class='control-group'>" +
                        "<label class='control-label' for='password'>Password</label>" +
                        "<div class='controls'>" +
                          "<input type='password' id='registerPassword' placeholder='Password'>" +
                        "</div><br></div>" +
                        "<div class='control-group'>" +
                        "<label class='control-label' for='rePassword'>Retype Password</label>" +
                        "<div class='controls'>" +
                          "<input type='password' id='rePassword' placeholder='Retype Password'>" +
                        "</div><br></div><div id='registerMsgbox'></div></div>" +
                        "<div class='col-md-3 column'>" +
                        "<b><u>Choose a Class</b></u><br><div id='characters'></div>" +
                        "</div></div>" +
                      "<div class='control-group'>" +
                        "<div class='controls'>" +
                          "<input name='Submit' type='button' onclick='javascript:register()' value='Register' class='btn btn-success'/>" +
                        "</div></div></form>";
       $.ajax({
        url : 'scripts/php/getCharacters.php',
        type : 'POST',
        data : {type:'classes'
        },
        dataType:'json',
        success : function(data) {              
           buildClasses(data);
        }
    });
}

function loginPage()
{
      document.getElementById("registerBox").innerHTML = "<form class='form-horizontal' method='post' action='' id='login_form'>" +
                      "<div class='control-group'>" +
                        "<label class='control-label' for='username'>Username</label>" +
                        "<div class='controls'>" +
                          "<input type='text' id='username' placeholder='Username'></div></div>" +
                      "<div class='control-group'> " +
                        "<label class='control-label' for='password'>Password</label>" +
                        "<div class='controls'>" +
                          "<input type='password' id='password' placeholder='Password'>" +
                        "</div><br>" +
                           "<div id='msgbox'></div></div>" +
                      "<div class='control-group'>" +
                        "<div class='controls'>" +
                          "<input name='Submit' type='button' onclick='javascript:login()' value='Login' class='btn btn-success'/>&nbsp;&nbsp;" +
                          "<input name='Submit' type='button' onclick='javascript:registration()' value='Register' class='btn btn-success'/>" +
                        "</div>" +
                      "</div>" +
                    "</form></div><div class='modal-footer'></div>";
}

function buildClasses(data)
{
    var characterDiv = "";
    for(var i = 0; i < data.length; i++)
    {
        characterDiv += "<div class='characterBox'><input type='radio' name='radioClasses' value='" + data[i].id + "'><img src='media/characters/heroTiles/" + data[i].pic + ".png'>" + data[i].name + "</div>";
    }
    document.getElementById("characters").innerHTML = characterDiv;
}

我也有一个注册脚本,它也没有对数据库做任何事情,但我想是为了减少覆盖页面的代码,发布可能不需要,因为我认为问题出在某处我的数据库函数类或其他一些地方。

2 个答案:

答案 0 :(得分:0)

为什么你认为它不与mysql交互?您是否尝试过使用浏览器开发者控制台(网络页面)来查看是否正在发送请求? 你能发布一些这些请求的截图吗?

答案 1 :(得分:0)

我解决了这个问题,事实证明这是由同一端口上的MySQL服务冲突引起的。 捂脸 对不起,如果我浪费你的时间。我们每隔一段时间都会犯下愚蠢的错误......