0

Here is my code:

$page = 'home';
$data = 'blah blah blah';
// my database connection (i know there is no problems here);
require_once('../inc/connection.php');
$id = 1;
$values = array(
    ':page' => $page,
    ':id' => $id,
    ':data' => $data
);
$query = 'UPDATE site SET :page=:data WHERE id=:id';
$sql = $conn->prepare($query);
$sql->execute($values);
// this prints out perfect
echo 'UPDATE site SET '.$page.'='.$data.' WHERE id='.$id;

When I run this page it prints the echo at the end, but when I check my site table it does not reflect the update. I have no idea what I am doing. The connection is good because I am making select queries just fine.

kqlambert
  • 2,693
  • 6
  • 33
  • 50

1 Answers1

2

Table and Column names cannot be replaced by parameters in PDO

so

$query = 'UPDATE `site` 
          SET `page`=:data 
          WHERE id=:id';

A query should do a specific thing, if you need dynamic columns then perhaps put them in an if or switch statement, or build a database model that handles this with class methods that handle column variations ect.

Community
  • 1
  • 1
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106