0

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 ?

2
  • so what's the exact problem what php/mysql errors are you getting? Commented Nov 20, 2014 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 Commented Nov 20, 2014 at 12:35

1 Answer 1

1

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."'
)
";
Sign up to request clarification or add additional context in comments.

1 Comment

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) !'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.