[英]Looping json data then send to database with php
I have json data like this : 我有这样的json数据:
myjsondata myjsondata
[
{
"id_user":"31"
},
{
"id_user":"32"
},
{
"id_user":"33"
}
]
then i send the data with jquery $.post 然后我用jQuery $ .post发送数据
$.post("myaction.php",
{send: myjsondata }, function(res) {
}, "json");
then in myaction.php, i decode the json and i want to send the data to database with foreach : 然后在myaction.php中,我对json进行解码,并且我想使用foreach将数据发送到数据库:
myaction.php myaction.php
$conn = mysqli_connect( "localhost","root","","mydb");
$data = json_decode($_POST['send']);
foreach($data as $row){
$id_user = $row->id_user;
}
mysqli_query($conn, "INSERT INTO tbl_user
(id_user) VALUES ('$id_user') ");
when i running that code, the data already inserted to the table , but the data only inserted with last id_user 当我运行该代码时,数据已经插入到表中,但是数据仅使用最后一个id_user插入
tbl_user tbl_user
id_user
33
i want all data is inserted into the table like this 我想像这样将所有数据插入表中
tbl_user tbl_user
id_user
31
32
33
how can i do that? 我怎样才能做到这一点? thanks 谢谢
Well, thats totally logic. 好吧,那完全是逻辑。 Lets take a look at your code: 让我们看一下您的代码:
$conn = mysqli_connect( "localhost","root","","mydb");
$data = json_decode($_POST['send']);
foreach($data as $row){
$id_user = $row->id_user;
}
mysqli_query($conn, "INSERT INTO tbl_user
(id_user) VALUES ('$id_user') "); // Here is the problem!!!!
In your foreach loop you overwrite the $id_user
variable. 在您的foreach循环中,您将覆盖$id_user
变量。 Then only at the end you insert. 然后仅在最后插入。 So what can you do? 所以,你可以做什么? Simply put the insert query inside the foreach loop and it will work. 只需将插入查询放在foreach循环中,它将起作用。
Working solution: 工作解决方案:
$conn = mysqli_connect( "localhost","root","","mydb");
$data = json_decode($_POST['send']);
foreach($data as $row){
$id_user = $row->id_user;
mysqli_query($conn, "INSERT INTO tbl_user
(id_user) VALUES ('$id_user') ");
}
You need to move the query into the for loop 您需要将查询移至for循环
$conn = mysqli_connect( "localhost","root","","mydb");
$data = json_decode($_POST['send']);
foreach($data as $row){
$id_user = $row->id_user;
mysqli_query($conn, "INSERT INTO tbl_user (id_user) VALUES ('$id_user') ");
}
1st: Move your query
into inside the foreach loop
第一:将query
移至foreach loop
foreach($data as $row){
$id_user = $row->id_user;
mysqli_query($conn, "INSERT INTO tbl_user (id_user) VALUES ('$id_user') ");
}
2nd : Try to use prepared statement 第二:尝试使用准备好的语句
Cause : your overwriting
$id_user
in foreach loop and your executing query after foreach loop so $id_user
only contains last row value only .so Move your query into inside the foreach loop
原因:您在foreach循环中overwriting
$id_user
在foreach循环之后执行查询,因此$id_user
仅包含最后一行的值。因此将查询移到foreach loop
myaction.php
File will be like this . myaction.php
文件将如下所示。
$conn = mysqli_connect( "localhost","root","","mydb");
$data = json_decode($_POST['send']);
foreach($data as $row){
$id_user = $row->id_user;
mysqli_query($conn, "INSERT INTO tbl_user (id_user) VALUES ('$id_user') ");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.