我正在尝试将用户信息插入MySQL。我创建了以下ajax调用,似乎是在JSON中创建数据,但是当我运行print_r( json_encode( $_GET));
时,它显示一个空数组。
在调试器中,似乎调用包含url中的数据:
results.php?method=submitScore&score=1&username=bob
main.html中
function getData(obj,callback){
$.ajax({
url: 'results.php',
type: "GET",
dataType: "json",
data: obj
}).done(function(data) {
if(callback != null)
callback(data);
});
}
results.php
<?php
class Results {
public $data;
public $success = true;
public $errorMessage;
}
print_r( json_encode( $_GET));
$func = $_GET['method'];
$obj = new Results();
switch ($func) {
case "getHighScores":
$conn = new mysqli('localhost','Username','Password','DB');
if (!$conn->connect_errno) {
$high = "SELECT * FROM HighScores ORDER BY Score LIMIT 10";
$scores = $conn->query($high);
$arr = array();
while ($row = $scores->fetch_assoc()){
$arr[] = $row;
}
$obj->data = $arr;
echo json_encode($obj);
}
break;
case "submitScore":
$userName = $_GET["username"];
$score = $_GET["score"];
$conn = new mysqli('localhost','W01214804','Kylecs!','W01214804');
if (!$conn->connect_errno) {
$query = "INSERT INTO HIGHSCORES VALUES('" . $userName . "', '" . $score . "')";
$conn->query($query);
}
break;
default:
echo "Invalid method";
break;
}
?>