I have a JSON array that is on a page. It is added to as time goes on by some user interaction. When the user is done they need to submit this array to a page where their information is added to a mySQL table via php. My call to the page via AJAX is as follows where answersArray is the array
$.ajax({
type: "POST",
dataType: "json",
data: answersArray,
url: 'sendToUsersFeedback.php',
});
The array looks like this:
[
{
"USERID": "3",
"INDVID": "0",
"RATING": "1",
"CONFIDENCE": "8"
},
{
"USERID": "3",
"INDVID": "1",
"RATING": "1",
"CONFIDENCE": "88"
}
]
This is where I get confused. I need to decode this incoming json and then loop through it added a new record for each array item (USERID, INDVID, RATING and CONFIDENCE make up one record etc..) I could have as many as 20 in this array. I know USERID is not unique. Already have that set up.
It is the php side I get messed up on. How do I decode an incoming array and go through it. I have tried the following
$sql = json_decode(data,true);
foreach( $data as $row ) {
$sql[] = '("'.mysql_real_escape_string($row['USERID']).'", '.$row['INDVID'].','.$row['RATING'].','.$row['CONFIDENCE'].')';
}
mysql_query('INSERT INTO users_feedback (USERID, INDVID, RATING. CONFIDENCE) VALUES '.implode(',', $sql));
Am I close? I'm very confused. Thanks in advance.
$_POSTvariable name. Should probably look at using'data':{'data':ansersArray}then change the PHP to usejson_decode($_POST['data'],true).