3

I need to dynamically generate the column name I need to update in Postgresql from PHP. Here's the code and the error:

$Col = "dog_".$Num."_pic";
$query_params = array(
        ':user_id_' => $CustomerID,
        'dog_path' => $filePath,
        'dog_col' => $Col)
        ;

$sql = "UPDATE users
        SET 
            `:dog_col`=:dog_path
        WHERE `username`=:user_id_";

I also tried pg_escape_string() with the string.

Here's the error.

"SQLSTATE[42S22]: Column not found: 1054 Unknown column ''dog_1_pic'' in 'field list'"}

1 Answer 1

4

You can't bind columns names in your query:

$sql = "UPDATE users 
        SET `:dog_col`=:dog_path
        WHERE `username`=:user_id_";

In this case you must use a variable like this:

    $column = 'myColumn';

    $sql = "UPDATE users
            SET $column = :dog_path
            WHERE username = :user_id_";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.