0

I have an Object from Javascript pass to PHP (via AJAX):

var jsObject = {
    "name": "Michael Divo",
    "age": 27,
    "country": "United States"
};

jsObject_json = JSON.stringify( jsObject );

$.ajax({
    type: "POST",
    dataType: 'json',
    url: "http://www.example.com/myserver.php",
    data: { mydata: jsObject_json },
}) ...

In, myserver.php:

$json = json_decode($_POST["mydata"], true);

Then if i save that $json into the MySQL from this PHP end, it is saved as:

Array

.. in the Database, as that "Array" as a String.


So how do i properly SAVE this JSON into the MySQL please? (So that i can later retrieve it back as a JSON and read.)

1
  • USe serialize function to store into DB. Commented Jun 12, 2014 at 12:30

5 Answers 5

3

If you just want to pull it back as JSON, then don't run it through json_decode. Leave it as a string.

Most sensible systems, however, would have a database structure that would allow each of the fields in the JSON submission to be handled separately. Then you would access each field and add it in its own column in the INSERT query. You could then perform queries on the data beyond "Give me all the data" (e.g. WHERE country = 'United States').

$preparedStatement = $db->prepare('INSERT INTO data (name, age, country) VALUES (:name, :age, :country)');
$preparedStatement->execute(json_decode($_POST["mydata"], true));
Sign up to request clarification or add additional context in comments.

Comments

0

You dont need to decode it before you store it into db, (store it as pure string), then if you what to get it back(somewhere else in your code) get that string and decode it.

Comments

0

By using json_encode() you can reencode your Array into a JSON that can be stored into a TEXT field in MySQL. Or since, you already have it as a string, leave it as is.

$stmt = $db->prepare("INSERT INTO FOO (mydata) VALUES (:my_data)");
$stmt->execute($_POST);

Comments

0

As your Database schema is not provided here so assuming that you need to store the string only, in this case don't decode your json as this will turn json string to array. In case of you have schema then you can proceed by decoding this into array and then save elements according to you table schema.

Comments

0

try this

$str = $db->prepare("INSERT INTO table (json_array) VALUES (:json_data)");
$str->execute($_POST["mydata"]);

6 Comments

Danger: You are vulnerable to SQL injection attacks that you need to defend yourself from.
@Quentin now every thing is ok
Except for not actually putting any placeholders in your query
sorry @Quentin i am not php developer,so i have no idea about PHP,but i edit again my answer.if something wrong in my answer so plz correct it.
And now you're vulnerable to SQL injection attacks again.
|

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.