简体   繁体   中英

Insert values from JSON into database

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.

First, give a name to your data you're posting :

$.ajax({
    type: "POST",
    dataType: "json",
    data: {
      my_data: answersArray
    },
    url: 'sendToUsersFeedback.php',
});

Then, in your PHP code, recover your data :

$data = $_POST['my_data'];
$sql = json_decode($data, true);
...

You should be aware that everybody can edit JSON in the client-side, so insert data into a database this way is extremely dangerous.

You are doing:

$sql = json_decode(data,true); 

What is data ? You probably missed the dollar sign. Are you actually getting your JSON in $data ?

You're assigning json_decode() result to $sql ; now your $sql variable will contain an associative array with your $data . $data is still a string because you didn't change it's value since retrieving it.

Then you're looping through the string (not happening) and running:

mysql_query('INSERT INTO users_feedback (USERID, INDVID, RATING. CONFIDENCE) VALUES '.implode(',', $sql));

$sql above will be your JSON, as a PHP array, something like:

array(array('USERID' => 1 ...), array('USERID' => 2 ...)); 

You should have:

$data = json_decode($data, true);
$sql = array();
foreach ($data as ...

Also, make sure you actually retrive $data:

$data = $_POST['data']

Simple, you have the foreach using the $data array that it's from the json code.

You must set the decoded array as another variable and loop with it.

$j_decoded = json_decode(data,true);

foreach(j_decoded 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));

Hope this helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM