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