I'm attempting to make a very basic browser MMORPG with php, ajax, and jquery. However the webpages do not seem to be interacting with the MySQL server correctly.
Here is my php class that defines some SQL functions:
<?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);
}
}
?>
Along with the config.ini it references:
[database]
username = root
password = ***
dbname = ***
(password and database name have been omitted but I've checked that they are correct.)
Here is my validation.php file that the login uses:
<?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");
}
?>
And here is the login javascript file with the functions that are called on my index page.
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'/> " +
"<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;
}
I also have a registration script that doesn't do anything to the database either, but I figure for the sake of having less code covering the page that posting that probably isn't required, since I assume the issue is somewhere in my database function class or one of these other places.
Aucun commentaire:
Enregistrer un commentaire