1

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

[
{
    "input": "calleFiscal",
    "valor": ""
},
{
    "input": "numFiscal",
    "valor": "numero fiscal"
},
{
    "input": "colFiscal",
    "valor": ""
},
{
    "input": "delefacionFiscal",
    "valor": ""
},
{
    "input": "estadoFiscal",
    "valor": "11"
},
{
    "input": "calleComercial",
    "valor": "calle comercial"
},
{
    "input": "numComercial",
    "valor": ""
},
{
    "input": "colComercial",
    "valor": ""
},
{
    "input": "delefacionComercial",
    "valor": ""
},
{
    "input": "estadoComercial",
    "valor": "3"
},
{
    "input": "calleEntrega",
    "valor": ""
},
{
    "input": "numEntrega",
    "valor": ""
},
{
    "input": "colEntrega",
    "valor": "colonia entrega"
},
{
    "input": "delefacionEntrega",
    "valor": ""
},
{
    "input": "estadoEntrega",
    "valor": "11"
}
]

是通过使用 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、Municipio、Estado、Tipo)然后我需要在表中插入 3 行,这 3 行具有相同的Id_Cliente但有不同您的 5 个输入的提示和不同内容。但不起作用,并显示此错误:

Tried to bind parameter number 0. SQL Server supports a maximum of 2100 parameters.

也许我的方法是错误的,如果有任何方法可以做到这一点,我很感激。

已编辑

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

4

1 回答 1

1

我认为您需要再添加 1 个计数器,以便在 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 示例 - http://phpfiddle.org/main/code/k4b-nja

于 2013-07-21T05:17:44.917 回答