简体   繁体   English

JSON对象数组MySQL插入

[英]Json object array mysql insert

I have the following json object: 我有以下json对象:

[Object { name="tag[]", value=""w1","x1","y1","z1""}, Object { name="tag[]", value=""w2","x2","y2","y3""}]

In php I'm able to store them in an array variable by using post like this: 在php中,我可以通过使用post将它们存储在数组变量中,如下所示:

  $tags[] = $_POST['tags'];
  $postid = 1

I want to insert the data like so: 我想这样插入数据:

    Insert Into tagtable  ( postid, w, x, y, z, ,0) values ( $postid, "w1", "x1", "y1", "z1"), ($postid, "w2", "x2", "y2", "z2")


 My problem is getting the comma separated values from the $tags variable.

I'm thinking of using a for loop that iterates through the size of the $tags array. 我正在考虑使用for循环遍历$ tags数组的大小的循环。 Can anyone direct me on how to construct this loop so that I can get a variable like 任何人都可以指导我如何构造此循环,以便获得类似

   $alltags = '( $postid, "w1", "x1", "y1", "z1"), ($postid, "w2", "x2", "y2", "z2")';

try this code .... its depend upon the size of tags array.... 试试这个代码....取决于标签数组的大小....

  $value="";
for($i=0;$i<sizeof($tags);$i++)
{
     $value .= "($postid,";
    for($j=0;$j<sizeof($tag[$i]);$j++)
    {
       $value .="'$tag[$i][$j]',";
    }
    rtrim($value, ",");
    $value .="),";
}
rtrim($value, ",");
$query = "Insert Into tagtable  ( postid, w, x, y, z, ,0) values".$value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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