JQuery Ajax 不与 MySQL 交互



我正在尝试用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);
}
}
?>

除了配置.ini它还引用:

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

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

这是我的验证.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;
}

我还有一个注册脚本,它也不会对数据库执行任何操作,但是我认为为了减少覆盖可能不需要发布的页面的代码,因为我认为问题出在我的数据库函数类中的某个地方或这些其他地方之一。

为什么

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

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

相关内容

  • 没有找到相关文章

最新更新