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.)

夏期劇場
  • 17,821
  • 44
  • 135
  • 217

5 Answers5

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));
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
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.

Sabri Aziri
  • 4,084
  • 5
  • 30
  • 45
0

try this

$str = $db->prepare("INSERT INTO table (json_array) VALUES (:json_data)");
$str->execute($_POST["mydata"]);
Gabber
  • 7,169
  • 3
  • 32
  • 46
  • 4
    **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jun 12 '14 at 12:25
  • @Quentin now every thing is ok – Gabber Jun 12 '14 at 12:31
  • Except for not actually putting any placeholders in your query – Quentin Jun 12 '14 at 12:34
  • 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. – Gabber Jun 12 '14 at 12:41
  • 2
    And now you're vulnerable to SQL injection attacks again. – Quentin Jun 12 '14 at 12:45
  • I suggest you delete it. Other people have made your point better already. – Quentin Jun 12 '14 at 12:52
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);
greut
  • 4,305
  • 1
  • 30
  • 49
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.

Rohit Choudhary
  • 2,253
  • 1
  • 23
  • 34