PDO bindParam error

Asked

Viewed 162 times

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 elses 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?

  • 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)

  • vc da bind twice ...

  • 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?

  • 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.

  • Hit, I put echo in sql, I have two :column and two :value, my column array and values have two elements each.

  • Inside that for(the second), just leave $stmt->bindParam(":coluna{$i}", $valores[$i]);

  • also failed, says that there is still a bind, the :value (I put in the first for

  • 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.

  • You can show how to call this function/method ?

  • I edited in the post, and managed to solve after rereading the entire function etc, I will post the reply, thanks for the attention.

Show 6 more comments

1 answer

0


The error is found in the use of bind, which is supported only for values and not campos(columns) from the database, so the right code would look like this:

First for:

for($i = 0; $i < $totalValores; $i++)
{
    $expressao = $expressao."{$colunas[$i]}=:valor{$i},";                        
}

According to for:

for($i = 0; $i < $totalValores; $i ++)
{ //substitui o bind criado
   $stmt->bindParam(":valor{$i}", $valores[$i]);
}

The correct query would look like this:

UPDATE usuarios SET nome=:valor0,senha=:valor1 WHERE id = '24' //Antes do bind 
UPDATE usuarios SET nome='uuuu',senha='ssss' WHERE id = '24' //Depois do bind 

And not like before:

UPDATE usuarios SET :coluna0=:valor0,:coluna1=:valor1 WHERE id = '24'

For, as I have already mentioned, no use of bind in database fields or columns.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.