4
I’m creating a CRUD dynamic in Pdo, where I receive several parameters within an array, and within that array there may be several other arrays. The problem is appears when I am mounting SQL.
I asked a similar question here, but the solution was not standardized, it is accusing error:
#5 {main}string(100) "SQLSTATE[HY093]: Invalid Parameter number: number of bound variables does not match number of tokens"
My PHP code is like this:
$campos = json_decode($item->TXT_COLUN_SINCR, true);
$campos = implode(' = ?, ', array_keys($campos));
$valores = json_decode($item->TXT_COLUN_SINCR, true);
$sql = sprintf("UPDATE %s SET %s %s ", $item->TXT_TABLE_SINCR, $campos, $item->TXT_WHERE_SINCR);
$sincronismo = $this->conexao->save($sql, $valores);
My $fields array:
Array
(
[COD_IDENT_IGREJ] => IBM
[COD_IDENT_CELUL] => 1
[COD_IDENT_PESSO] => 120151202162837
[DAT_INICI_PARTC] => 0000-00-00
[FLG_IDENT_PESSO] => M
[FLG_STATU_PARTC] => A
[DAT_FINAL_PARTC] =>
[MEM_OBSRV_ADCNS] =>
[COD_IDULT_ATUAL] => 1
[DAT_ULTIM_ATUAL] => 2015-12-05 15:14:26
[COD_CELUL_PESSO] => 4
)
Details for parameter error:
Array
(
[COD_IDENT_IGREJ] => IBM
[COD_IDENT_CELUL] => 1
[COD_IDENT_PESSO] => 120151202162837
[DAT_INICI_PARTC] => 0000-00-00
[FLG_IDENT_PESSO] => M
[FLG_STATU_PARTC] => A
[DAT_FINAL_PARTC] =>
[MEM_OBSRV_ADCNS] =>
[COD_IDULT_ATUAL] => 1
[DAT_ULTIM_ATUAL] => 2015-12-05 15:14:26
[COD_CELUL_PESSO] => 4
)
PDOStatement Object
(
[queryString] => UPDATE tbl_PESSOA_CELULA SET COD_IDENT_IGREJ = ?, COD_IDENT_CELUL = ?, COD_IDENT_PESSO = ?, DAT_INICI_PARTC = ?, FLG_IDENT_PESSO = ?, FLG_STATU_PARTC = ?, DAT_FINAL_PARTC = ?, MEM_OBSRV_ADCNS = ?, COD_IDULT_ATUAL = ?, DAT_ULTIM_ATUAL = ?, COD_CELUL_PESSO= ? WHERE COD_IDENT_PESSO = '120151202162837' and COD_IDENT_CELUL = '1' and COD_IDENT_IGREJ = 'ibm'
)
What mistake you get ?
– Edilson
#5 {main}string(282) "SQLSTATE[42000]: Syntax error or access Violation: 1064 You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near ''ibm' '120151202162837'"
– Renan Rodrigues
Only that actually the problem is when mounting the query
– Renan Rodrigues
Do the following in the method
save
, comments on the lines that follow the$stmt = $con->prepare($sql);
, and puts avar_dump($stmt)
.– Edilson
The result is that sql I put up. The problem is happening when I’m generating sql. I will comment on the line, I know it is but I do not know how to solve.
– Renan Rodrigues
He’s riding sql like this
UPDATE tbl_PESSOAS SET VALOR 1, VALOR 2, :solucao1, solucao2
should look like thisUPDATE tbl_PESSOA SET valor 1 = :solucao1, valor 2 = :solucao2
But this is what I’m failing to develop.– Renan Rodrigues
$campos = explode(',', $campos); $binds = explode(',', $binds);
and then do thisvar_dump(array_combine($binds, $campos))
.– Edilson
OK I will test, that to make an answer in case anyone has doubt ?
– Renan Rodrigues
It would be unnecessary to answer without being sure that this was the solution. I’ll wait for your return, and then we’ll see if this is the solution or if there’s more.
– Edilson
It really didn’t work
– Renan Rodrigues
@Edilson I edited the question for you to see the result
– Renan Rodrigues
You are riding wrong.
– Edilson
Yes, the difficulty is there
– Renan Rodrigues
Vale remembers that I already have an array where I have the [VALUE] = SOLUTION
– Renan Rodrigues
As I described in the previous question of my example. Take a look at it.
– Renan Rodrigues
The select syntax is wrong, the problem is neither PHP nor PDO. Try to select manually directly in DB.
– Bacco
The way this is going is kind of tricky to work, adding pieces of code where there shouldn’t even be more code, try this - http://pastebin.com/bhsbVxT8
– Edilson
What is the PK(field) to update?
– rray