Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP PDO解码后将整个JSON插入数据库_Php_Json_Pdo_Foreach_Insert - Fatal编程技术网

使用PHP PDO解码后将整个JSON插入数据库

使用PHP PDO解码后将整个JSON插入数据库,php,json,pdo,foreach,insert,Php,Json,Pdo,Foreach,Insert,我想从jquery.ajax中插入这个json(index VALOR的内容并不总是有数据): 是通过使用jquery映射div创建的,我尝试使用以下命令插入数据库: $addresses = json_decode($this->dataActionClient['addresses'],true); $sqlAd = "INSERT INTO t_direcciones_clientes (Id_Cliente,Calle,Numero,Colonia,Municipio,Estad

我想从jquery.ajax中插入这个json(index VALOR的内容并不总是有数据):

是通过使用jquery映射div创建的,我尝试使用以下命令插入数据库:

$addresses = json_decode($this->dataActionClient['addresses'],true);

$sqlAd = "INSERT INTO t_direcciones_clientes (Id_Cliente,Calle,Numero,Colonia,Municipio,Estado,Tipo) VALUES (:idc,:calle,:num,:col,:deleg,:edo,:tipo)";
$resultAd = $this->dbConnect->prepare($sqlAd) or die ($sqlAd);

$fields = array('calle','num','col','deleg','edo');
$types = array('fiscal','comercial','entrega');

$resultAd->bindParam(':idc',$id_cliente,PDO::PARAM_INT);
$counType = 0;

foreach ($addresses as $key => $value) {
    $key++;
    $resultAd->bindParam(':'.$fields[$key], $value['valor']);
    if ($key == 4 || $key == 9) {
        $resultAd->bindParam(':tipo', $types[$counType]);
        $counType++;
        $resultAd->execute();
    }
}
对该守则的解释:

我有3个区域(财政、商业、entrega),每个区域有5个输入(Calle、Numero、Colonia、市政、Estado、Tipo),然后我需要在表中插入3行,这3行具有相同的客户Id,但具有不同的Tipo,以及您5个输入的不同内容。 但不起作用,并显示此错误:

Tried to bind parameter number 0. SQL Server supports a maximum of 2100 parameters.
也许我的方法是错误的,如果有任何方法可以做到这一点,我很感激

已编辑


我解决了根据系统功能更改某些值的问题,但要感谢大家。

我认为您需要再添加一个计数器,在5次循环后可以重置为0。试试这个-

$counField = 0; // counter to access field array, will be reset to 0 after 5 loops
$counType = 0;

foreach ($addresses as $key => $value) {
    $resultAd->bindParam(':'.$fields[$counField], $value['valor']);
    if ($key == 4 || $key == 9 || $key == 14) {
        $resultAd->bindParam(':tipo', $types[$counType]);
        $counType++;
        $counField++; // increase if the last of 5 loops
        $resultAd->execute();
    }
    else {
        $counField++; // increase if not the last of 5 loops 
    }
}

这里是一个phpFiddle示例,显示了结果-

我认为您需要再添加一个计数器,在5次循环后可以重置为0。试试这个-

$counField = 0; // counter to access field array, will be reset to 0 after 5 loops
$counType = 0;

foreach ($addresses as $key => $value) {
    $resultAd->bindParam(':'.$fields[$counField], $value['valor']);
    if ($key == 4 || $key == 9 || $key == 14) {
        $resultAd->bindParam(':tipo', $types[$counType]);
        $counType++;
        $counField++; // increase if the last of 5 loops
        $resultAd->execute();
    }
    else {
        $counField++; // increase if not the last of 5 loops 
    }
}

下面是一个phpFiddle示例,显示了结果-

Sure。。稍等一下,我忘了再放两行了。您正在使用问号占位符-
,但创建/使用命名占位符
:'。$fields[$key]
??另外,
bindParam()
使用1-index,其中数组是0-index(我认为您的错误是
试图绑定参数编号0
),因此您可能需要将键数增加1。嗯,我的第一条评论是错误的,您没有绑定2个参数,但是5,当$key点击4时,将执行该语句,所以你缺两个。。。还有@Sean所说的;)我更新我的代码@SeanI更新我的代码@dbfSure。。稍等一下,我忘了再放两行了。您正在使用问号占位符-
,但创建/使用命名占位符
:'。$fields[$key]
??另外,
bindParam()
使用1-index,其中数组是0-index(我认为您的错误是
试图绑定参数编号0
),因此您可能需要将键数增加1。嗯,我的第一条评论是错误的,您没有绑定2个参数,但是5,当$key点击4时,将执行该语句,所以你缺两个。。。还有@Sean所说的;)我更新我的代码@SeanI更新我的代码@dbfThanks,但我用不同的方法解决了我的答案,我在编辑过的帖子中发布了我的正确答案。但是谢谢你的时间。谢谢,但是我用不同的方式解决了我的答案,我把我的正确答案贴在编辑过的帖子上。但是谢谢你抽出时间。