0
I have a function of UPDATE
which takes table, column array, values and Where, I do the treatment:
//montar SQL
$totalValores = count($valores); //conta quantos valores
$expressao = null;
for($i = 0; $i < $totalValores; $i++)
{
$expressao = $expressao.":coluna{$i}=:valor{$i},"; //monta minha expresao de bind :coluna0=:valor0 e assim por diante
}
$expressao = substr($expressao, 0, -1); // remove a última virgula
$tabela = "UPDATE ".$tabela." "; // vai montando minha sql
$expressao = " SET ".$expressao." ";// vai montando minha sql
if($where)
{
if(!strstr($where, "'"))
{ //trata meu where caso ele venha sem aspas ''
$arr1 = preg_split("/([^\w\s]+\s*)/", $where, -1, PREG_SPLIT_DELIM_CAPTURE);
$where = "WHERE ".$arr1[0].$arr1[1]." '".$arr1[2]."' ";
}
else
$where = "WHERE ".$where;
}
else
$where = NULL;
$sql = $tabela.$expressao.$where; // monta sql (ate aqui tudo bem)
Further ahead I have:
echo $sql = $tabela.$expressao.$where; //verifica sql
if($conn = conectar()) // se conectar
{
$stmt = $conn-> prepare( $sql ); //prepara
for($i = 0; $i < $totalValores; $i ++)
{ //substitui os binds criados
$stmt-> bindParam(":coluna{$i}", $colunas[$i]);
$stmt-> bindParam(":valor{$i}", $valores[$i]);
}
$result = $stmt->;execute(); // executa O ERRO ESTA AQUI
if ( ! $result )
{
var_dump( $stmt->;errorInfo() );
exit;
}
echo "<br> ;Atualizado!</br> ;";
$conn = null;
return true;
}
The part I omitted from the code is only else
s for error handling, when calling so:
atualizar("usuarios",
array("nome", "senha"),
array("uuuu", "ssss"),
"id = 24");
I have the mistakes
Fatal error: in C: wamp www php test update.php on line 79
and
( ! ) Pdoexception: in C: wamp www php test update.php online 79 Call Stack
# Time Memory Function Location
1 0.0012 133528 {main}( ) .. index.php:0
2 0.0055 174544 update( ) .. index.php:17
3 0.1442 183776 execute ( ) .. update.php:79
Line 79 is just mine execute()
, can anyone help me? I’ve done this successfully in the insertion function, and this is practically copy and glue it, only here I have two bindParam
bind you in columns and values?
– rray
Yes. Down there is the row I bind in :column$i turning into the value of $columns[$i] and respectively with values, and both have two elements (name and password)
– Leonardo
vc da bind twice ...
– rray
Yes, can not? In one example the internet was with two Binds
:nome
:email
.. In this case I would have to "concatenate" the two arrays being [0] =column and [1] =value? To do so in a bind?– Leonardo
I suspect that the number of Binds does not match the number of values passed, the error of your question seems an exception ... that did not return any useful message,
$stmt->errorInfo()
should return something very specific and a Sqlstate.– rray
Hit, I put echo in sql, I have two :column and two :value, my column array and values have two elements each.
– Leonardo
Inside that for(the second), just leave
$stmt->bindParam(":coluna{$i}", $valores[$i]);
– rray
also failed, says that there is still a bind, the :value (I put in the first for
– Leonardo
I also did as I told you, joining the two arrays where Dice par would be my column and Dice impar would be my value, so having only one bind, but continues the error.
– Leonardo
You can show how to call this function/method ?
– rray
I edited in the post, and managed to solve after rereading the entire function etc, I will post the reply, thanks for the attention.
– Leonardo