PDO bindParam error

0

I have a function of UPDATE that receives table, array of columns, 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)

More 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, by calling:

atualizar("usuarios",
         array("nome", "senha"),
         array("uuuu", "ssss"),
         "id = 24");

I have the errors

  

Fatal error: in C: \ wamp \ www \ test \ php \ update.php on line 79

and

  

(!) PDOException: in C: \ wamp \ www \ test \ php \ update.php on line    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 my execute() , can anyone help me? I've done this successfully in the insert function, and this is practically copy and paste it, but here I have two bindParam

    
asked by anonymous 24.12.2015 / 00:40

1 answer

0

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

First for :

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

Second 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 as it was before:

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

Well, as I've already mentioned, not allowed to use bind in fields or columns of the database.

    
24.12.2015 / 04:32