Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Hi I'm trying to insert the json array into my MySQL database. I'm passing the data form my iphone there i have converted the data into json format and I'm passing the data to my server using the url its not inserting into my server.

This is my json data.

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

This is my Php code.

<?php 

 $json = file_get_contents('php://input');
 $obj = json_decode($data,true);

 //Database Connection
require_once 'db.php';

 /* insert data into DB */
    foreach($obj as $item) {
       mysql_query("INSERT INTO `database name`.`table name` (name, phone, city, email) 
       VALUES ('".$item['name']."', '".$item['phone']."', '".$item['city']."', '".$item['email']."')");

     }
  //database connection close
    mysql_close($con);

   //}
   ?>

My database connection code.

   <?php

       //ENTER YOUR DATABASE CONNECTION INFO BELOW:
         $hostname="localhost";
         $database="dbname";
         $username="username";
         $password="password";

   //DO NOT EDIT BELOW THIS LINE
     $link = mysql_connect($hostname, $username, $password);
     mysql_select_db($database) or die('Could not select database');
 ?> 

Please tell where I'm doing wrong in the above code basically I'm not a php developer I'm mobile application developer so I'm using the php as a server side scripting please tell me how to resolve this problem.

share|improve this question
1  
Also show us the content of db.php. – Amit Mar 20 '14 at 4:34
    
Your code looks good i.e. parsing JSON and insert query I suppose its related to connection and db select. Use mysql_error() after the query to see what happened. – Abhik Chakraborty Mar 20 '14 at 4:37
    
I think you have problem with the connection. As Amit said, show your db.php code – 4EACH Mar 20 '14 at 4:37
    
use error_reporting(E_ALL); – 4EACH Mar 20 '14 at 4:42
    
@amit have updated my db connection code in my question pls check it – user3427551 Mar 20 '14 at 4:42
up vote 5 down vote accepted
 $json = file_get_contents('php://input');
 $obj = json_decode($json,true);

I think ur passing wrong variable. you should pass $json in json_decode as above..

share|improve this answer

There is no such variable as $data. Try

$obj = json_decode($json,true);

Rest looks fine. If the error still persists, enable error_reporting.

share|improve this answer
    
good catch !! even I tried his code locally but I had it correct while checking it so missed that completely that he was using wrong variable name !! – Abhik Chakraborty Mar 20 '14 at 4:40

You are missing JSON source file. Create a JSON file then assign it to var data:

<?php

require_once('dbconnect.php');

// reading json file
$json = file_get_contents('userdata.json');

//converting json object to php associative array
$data = json_decode($json, true);

// processing the array of objects
foreach ($data as $user) {
    $firstname = $user['firstname'];
    $lastname = $user['lastname'];
    $gender = $user['firstname'];
    $username = $user['username'];

    // preparing statement for insert query
    $st = mysqli_prepare($connection, 'INSERT INTO users(firstname, lastname, gender, username) VALUES (?, ?, ?, ?)');

    // bind variables to insert query params
    mysqli_stmt_bind_param($st, 'ssss', $firstname, $lastname, $gender, $username);

    // executing insert query
    mysqli_stmt_execute($st);
}

?>
share|improve this answer
    
What does this offer over the other two answers here? – Martijn Pieters Nov 30 at 13:33
$string=mysql_real_escape_string($json);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.