tags:

views:

24

answers:

1

I can't get bindValue to bind my values to the sql query.

 $sql = "INSERT INTO :table(:columns) VALUES(:values)";
 $query = $conn->prepare($sql);
 $query->bindValue(':table',$table);
 $query->bindValue(':columns',$columns);
 $query->bindValue(':values',$values);
 $query->execute();

When I run this, $query->execute() returns "false" and the data isn't update to the DB. I've also tried:

 $sql = "INSERT INTO :table(:columns) VALUES(:values)";
 $query = $conn->prepare($sql);
 $query->execute(array('table'=>$table,':columns'=>$columns,':values'=>$values));

and it still doesn't work.

This works but isn't what I want to do:

 $sql = "INSERT INTO $table($columns) VALUES($values)";
 $result = $conn->query($sql);

Please tell me what I'm doing wrong. Thanks.

+2  A: 

You are using it incorrectly, you cannot dynamically assign structural SQL values etc via the bindParam as it is meant for column values being inserted in / updated / tested against.

UPDATE

If you provide us with what the $columns and $variables (as Col. Shrapnel asked in the comments) contents generally are / where they come from, I / we maybe able to help you with a work around to your predicament.

Brad F Jacobs
$table = "notes";$columns = "SchedNote";$values = "Test insert";
Lyle Crane
What happens if you have two columns? How is that formatted, as expected with a comma? How about two values?
Brad F Jacobs