[英]Insert values from JSON into database
I have a JSON array that is on a page. 我在页面上有一个JSON数组。 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.
当用户完成操作后,他们需要将此数组提交到一个页面,在该页面上,他们的信息将通过php添加到mySQL表中。 My call to the page via AJAX is as follows where
answersArray
is the array 我通过AJAX对页面的调用如下,其中
answersArray
是数组
$.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.
我需要解码此传入的json,然后遍历整个数组,为每个数组项添加一条新记录(USERID,INDVID,RATING和CONFIDENCE组成一个记录等。)我在此数组中最多可以有20个。 I know USERID is not unique.
我知道USERID不是唯一的。 Already have that set up.
已经设置好了。
It is the php side I get messed up on. 这是我搞砸的PHP方面。 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 : 然后,在您的PHP代码中,恢复数据:
$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. 您应该注意,每个人都可以在客户端编辑JSON,因此以这种方式将数据插入数据库非常危险。
You are doing: 您正在执行:
$sql = json_decode(data,true);
What is data
? 什么是
data
? You probably missed the dollar sign. 您可能错过了美元符号。 Are you actually getting your JSON in
$data
? 您实际上是在
$data
获取JSON吗?
You're assigning json_decode() result to $sql
; 您正在将json_decode()结果分配给
$sql
; now your $sql
variable will contain an associative array with your $data
. 现在,您的
$sql
变量将包含与$data
的关联数组。 $data
is still a string because you didn't change it's value since retrieving it. $data
仍然是一个字符串,因为自检索以来您没有更改它的值。
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: 上面的
$sql
将是您的JSON,作为一个PHP数组,如下所示:
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:
$data = $_POST['data']
Simple, you have the foreach using the $data array that it's from the json code. 很简单,您可以使用来自json代码的$ data数组进行foreach操作。
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. 希望这对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.