Using prepared statements with SQLite3 and PHP Using prepared statements with SQLite3 and PHP sqlite sqlite

Using prepared statements with SQLite3 and PHP


You managed to confuse binding functions.

It is bindParam have to be used if you don't have your variable assigned yet.
While bindValue have to be used with existing value only.

Also, you should turn error reporting ON


You don't need intermediate variables, you must do this:

$smt = $db->prepare("insert into names (name, email) values (':name', ':email')");$smt->bindValue(':name', $_POST['post_name'], SQLITE3_TEXT);$smt->bindValue(':email', $_POST['post_email'], SQLITE3_TEXT);$smt->execute();

As documented in SQLite3Stmt::bindValue() value is binded instantly, not as SQLite3Stmt::bindParam() that gets the value of the variable at execute() time. So the problem is that that variables are empty when the statement is executed.


Remember:

  • You don't need to add parentheses on variable assignment: $a = ($b); -> $a = $b;
  • You MUST quote variable key name. Otherwise PHP will try to look for a constant with this name and will throw a warning if it doesn't exists... but will assign a erroneous key value if it exists!! $_POST[post_name] -> $_POST['post_name']