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.

I am decoding JSON array and then using foreach to loop through after that inserting data to mysql... but always getting "Cannot save data" !

see below php script to know what i have tried yet:

<?php

$objConnect = mysql_connect("localhost","","");
$objDB = mysql_select_db("test");

$raw_json = <<<EOT
{"data":[
{"PersonName":"first user","PersonEmail":"[email protected]"},
{"PersonName":"second user","PersonEmail":"[email protected]"}
]}
EOT;

// $raw_json = $_POST["allData"]; -- passing parameter

$json = json_decode($raw_json);
// echo json_encode($json); --- getting

foreach($json->data as $item){
// echo json_encode($item); --- getting
  $strPersonName = $item->PersonName;
// echo json_encode($strPersonName); --- getting
  $strPersonEmail = $item->PersonEmail;
// echo json_encode($strPersonEmail); --- getting

/*** Insert ***/
$strSQL = "insert into person (PersonName,PersonEmail)
values (
'".$strPersonName."',
'".$strPersonEmail."',
)
";

$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
$arr['StatusID'] = "0";
$arr['Message'] = "Cannot save data";   
}
else
{
$arr['StatusID'] = "1";
$arr['Message'] = "Data stored successfully";
}
}

mysql_close($objConnect);
echo json_encode($arr);
?>

and when i use below php script, i am able to store data to server, check this:

<?php
$objConnect = mysql_connect("localhost","","");
$objDB = mysql_select_db("test");

$_POST["sPersonName"] = "demo";
$_POST["sPersonEmail"] = "[email protected]";

$strPersonName = $_POST["sPersonName"];
$strPersonEmail = $_POST["sPersonEmail"];

/*** Insert ***/
$strSQL = "INSERT INTO person (PersonName, PersonEmail) 
    VALUES (
        '".$strPersonName."',
        '".$strPersonEmail."'
        )
    ";

$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
    $arr['StatusID'] = "0"; 
    $arr['Message'] = "Cannot save data!";  
}
else
{
    $arr['StatusID'] = "1"; 
    $arr['Message'] = "Data stored successfully";   
}

/**
    $arr['StatusID'] // (0=Failed , 1=Complete)
    $arr['Error'] // Error Message
*/

mysql_close($objConnect);

echo json_encode($arr);
?>

So what could be the reason? why i am not able to store data to mysql table, when i am decoding json array ?

share|improve this question
    
so what's the exact problem what php/mysql errors are you getting? –  andrew Nov 20 '14 at 12:29
    
Please, please, please Read the READ BOX: mysql is deprecated. Stop using it. Use an extension that supports prepared statements if you even remotely care about injection –  Elias Van Ootegem Nov 20 '14 at 12:35

1 Answer 1

up vote 2 down vote accepted

You have a comma after the last value...

Change

$strSQL = "insert into person (PersonName,PersonEmail)
values (
'".$strPersonName."',
'".$strPersonEmail."',
)
";

into

$strSQL = "insert into person (PersonName,PersonEmail)
values (
'".$strPersonName."',
'".$strPersonEmail."'
)
";
share|improve this answer
    
i have not noticed that anyways thanks for point out my mistake :) i already ticked as useful and very soon i will accept your answer (in next 8 minutes) !' –  Sun Nov 20 '14 at 12:32

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.