1

Hi I'm trying to insert the json array into my MySQL database I'm new for this php development i have seen so many methods that all little confusing please help me out.

This is my json data.

[{"name":"0","phone":"dsf","city":"sdfsdf","email":"dsf"},{"name":"13123123","phone":"sdfsdfdsfsd","city":"sdfsf","email":"13123123"}]

I want to inset this json array into my mysql database so i have used this code its not working please tell me how to make it done. My php code.

<?php 

  $json = file_get_contents('php://input');
  $obj = json_decode($data,true);
  $obj[0]['name'];
  $obj[1]['phone'];
  $obj[2]['city'];
  $obj[3]['email'];

 //Database Connection
  require_once 'db.php';

 /* insert data into DB */
      mysql_query("INSERT INTO `db512172115`.`trial` (name, phone, city, email) 
      VALUES ('".$obj->{'name'}."', '".$obj->{'phone'}."', '".$obj->{'city'}."', '".$obj->{'email'}."')");

 //database connection close
   mysql_close($con);

   //}
  ?>

This above code only have used to store the values into my database please help me out to resolve this problem.

Thanks.

user3427551
  • 347
  • 1
  • 7
  • 17
  • 1
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Mar 19 '14 at 16:59

2 Answers2

1

Within your json_decode function you've specified 'true', this will make return an array.

You're then trying to access this information as an object, which won't work. Plus the fact that you are given two arrays back would suggest that you're going to need a foreach statement to insert both pieces of data in to the database.

Have another look in to foreach statements and arrays, there's plenty of information around to give you a hand.

For example:

$array = json_decode($data,true);

foreach($array as $item) {
    mysql_query("INSERT INTO `db512172115`.`trial` (name, phone, city, email) 
  VALUES ('".$item['name']."', '".$item['phone']."', '".$item['city']."', '".$item['email']."')");

}

To add on to this, you're using a deprecated mysql_* function, you should look in to using mysqli or PDO for increased security.

Karl
  • 5,435
  • 11
  • 44
  • 70
0

This is another easy way

<?php
    $json_value = file_get_contents('your_json_data_link');
    $array = json_decode($json_value,true);
    require_once 'db.php';
    foreach($array as $item) {
        $insert_value = "INSERT INTO `db512172115`.`trial` (name, phone, city,email)VALUES 
        ('".$item['name']."', '".$item['phone']."', '".$item['city']."', '".$item['email']."')");

        if ($con->query($insert_value ) === TRUE) {
            echo "Record Successfully<br>";
        }
        else
        {
            echo "Error: " . $insert_value . "<br>" . $con->error;
        }
    }
?>
A.A Noman
  • 5,244
  • 9
  • 24
  • 46