0

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.

5
  • Your json object references location and you reference loc in your php. Look at var_dump($planet) to check what you want to insert/bind. Commented Feb 21, 2016 at 13:45
  • typo for this question. Regardless, its not the error because the code breaks when trying to bind the first parameter ("owner") Commented Feb 21, 2016 at 13:48
  • Can you post the output of var_dump($plant) - you may still reference it wrong and thus the bind fails. Commented Feb 21, 2016 at 13:49
  • See OP edit, i added it. Commented Feb 21, 2016 at 14:01
  • You need to add ,true to your JSON_decode call if you want to use it as an associative array. See here Commented Feb 21, 2016 at 14:18

2 Answers 2

2

if it's object I think you can call like $planet->owner not like array $planet['owner'].

Sign up to request clarification or add additional context in comments.

1 Comment

Holy crap, so much wasted time for such a blatant error. thanks.
1

You have decoded the data into an stdObject type, have it like the following:

if (isset($_POST["planet"]{
    $planet = JSON_decode($_POST["planet"], true);
}

This way you have the object as an array. stdObject means that you can access your data like: $planet->owner rather than $planet["owner"]. That's what the second true param for json_decode is used for.

2 Comments

Thanks. The other guy was quicker, but i gave you +1. Thanks !
No problem, though my solution works without having to modify any bit of your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.