Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PDO: Why cause this error in sql statement?

Tags:

sql

php

pdo

Well I dont understand why dont execute correctly this line:

try {
    $sql = "UPDATE t_perfiles_permisos 
        SET :tipo = :valor
            WHERE Area_Permiso = :area AND Id_Perfil = :idp";
    $result = $this->dbConnect->prepare($sql) or die ($sql);
    $result->bindParam(':tipo',$this->tipo,PDO::PARAM_STR);
    $result->bindParam(':valor',$this->valor,PDO::PARAM_INT); 
    $result->bindParam(':area',$this->area,PDO::PARAM_STR);
    $result->bindParam(':idp',$this->idp,PDO::PARAM_INT);

    $result->execute();
} catch (PDOException $e) {
    echo "Error!! No se puede establecer el permiso: ".$e->getMessage()."<br/>";
    return false;
}

Error:

Error!! No se puede establecer el permiso: 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 ''Buscar' = '1' WHERE Area_Permiso = 'Perfiles' AND Id_Perfil = '4'' at line 2
like image 600
SoldierCorp Avatar asked Dec 17 '25 03:12

SoldierCorp


1 Answers

The problem is:

SET :tipo = :valor

You can only use bound parameters for values, not for column names.

What you need to do in this case, is use a normal variable in your sql statement and check that variable against a white-list of allowed column names.

SET `{$checked_against_whitelist_column_name}` = :valor
like image 172
jeroen Avatar answered Dec 19 '25 20:12

jeroen