I have this JS Object:
var planet = {
owner: 4,
name: "Mars",
loc: [3, 4],
level: 4,
baseIncome: 1000
}
I use ajax to transfer this object to PHP
postPlanets: function(planet){
planet = JSON.stringify(planet);
$.ajax({
type: "POST",
url: "postGameData.php",
datatype: "json",
data: {planet: planet},
error: ajax.error,
success: ajax.success,
});
I decode the JSON on php via
if (isset($_POST["planet"]{
$planet = JSON_decode($_POST["planet"]);
}
I then try to do an INSERT using PDO
public function insertPlanet($planet){
$stmt = $this->connection->prepare("
INSERT INTO planets
(owner, name, x, y, level, baseIncome)
VALUES
(:owner, :name, :x, :y, :level, :baseIncome)
");
$stmt->bindParam(":owner", $planet["owner"]);
$stmt->bindParam(":name", $planet["name"]);
$stmt->bindParam(":x", $planet["loc"][0]);
$stmt->bindParam(":y", $planet["loc"][1]);
$stmt->bindParam(":level", $planet["level"]);
$stmt->bindParam(":baseIncome", $planet["baseIncome"]);
$stmt->execute();
if ($stmt->errorCode() == 0){
return true;
}
else {
return false;
}
}
But its not working. I know it breaks once it tries to bind the very first parameter on PDO, but i dont know either way nor how i could possibly debug the prepared statement at all.
What am i doing wrong ?
ERROR RETURN
object(stdClass)#3 (5) {
["owner"]=>
string(3) "sdf"
["name"]=>
string(3) "sdf"
["loc"]=>
array(2) {
[0]=>
int(2)
[1]=>
int(4)
}
["level"]=>
string(3) "sdf"
["baseIncome"]=>
string(1) "f"
}
<br />
<b>Fatal error</b>: Cannot use object of type stdClass as array in <b>D:\SecureWAMP_Portable\htdocs\projectX\DBManager.php</b> on line <b>285</b><br />
285 is the very first BIND attempt.