user1293029
user1293029

Reputation:

Unable to prepare statement in SQLite

What's wrong with the following code?

( ! ) Warning: SQLite3::prepare() [sqlite3.prepare]: Unable to prepare statement: 1, near ":DB_NAME": syntax error in C:\xampp\htdocs\memo\DB.php on line 91

Call Stack

# Time Memory Function Location

1 1.0436 335032 {main}( ) ..\memo.php:0

2 1.0471 370312 memo->__construct( ) ..\memo.php:22

3 1.0524 371112 DB->addRow( ) ..\memo.php:17

4 1.0524 371240 prepare ( ) ..\DB.php

/**
 * Add row to DB table.
 * @return bool
 */
public function addRow($idVal, $titleStr, $contentStr){
    $query = "INSERT INTO :DB_NAME VALUES(:ID, :Title, :Content);";
    $stmt = $this->db->prepare($query);
    $stmt->bindValue(":DB_NAME", DB::DB_NAME);
    $stmt->bindValue(':id', $idVal, SQLITE3_INTEGER);
    $stmt->bindValue(":Title", $titleStr, SQLITE3_TEXT);
    $stmt->bindValue(":Content", $contentStr, SQLITE3_TEXT);

    return $stmt->execute();
}

Upvotes: 0

Views: 10005

Answers (1)

Gerald Schneider
Gerald Schneider

Reputation: 17797

You cannot use table or column names as placeholders in prepared statements. You can only bind values.

Use it like this:

$query = "INSERT INTO ". DB::DB_NAME ." VALUES(:ID, :Title, :Content);";

Upvotes: 6

Related Questions