Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 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 at 4:37
    
I think you have problem with the connection. As Amit said, show your db.php code –  4EACH Mar 20 at 4:37
    
use error_reporting(E_ALL); –  4EACH Mar 20 at 4:42
    
@amit have updated my db connection code in my question pls check it –  user3427551 Mar 20 at 4:42

2 Answers 2

up vote 3 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
    
thanks a lot amit it worked...... –  user3427551 Mar 20 at 4:46
    
accept the answer to mark it complete. –  amit Mar 20 at 4:47

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 at 4:40

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.