-3

I am trying to figure out how prepared statements work in PDO. I have the following file:

<?php


$user = "root";
$pass = "<removed for this post>";
$db = new PDO("mysql:host=localhost;dbname=pdo-demo", $user, $pass);

$stmt = $db->prepare("INSERT INTO pdo-demo (firstname, lastname, email) value (:firstname, :lastname, :email)");

$stmt->bindParam(':firstname', $firstname);
$stmt->bindParam(':lastname', $lastname);
$stmt->bindParam(':email', $email);

$firstname = "John";
$lastname = "Doe";
$email = "johndoe@nowhere123.com";
$stmt->execute();

$db = null;?>

When I enter the page nothing happens, what am I missing? Shouldn't it insert the data?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
crystyxn
  • 1,411
  • 4
  • 27
  • 59

2 Answers2

0

pdo-demo that translates to pdo minus demo And your using that name for database AND table.

  • both my db and table are called pdo-demo – crystyxn Feb 26 '17 at 14:49
  • 1
    [*"pdo-demo that translates to pdo minus demo"*](http://stackoverflow.com/questions/42469803/prepared-statement-pdo-does-nothing?noredirect=1#comment72080427_42469803) - If you're going to post an answer based on a comment, you really should be quoting the person who wrote that comment up there, mainly *"moi"*. – Funk Forty Niner Feb 26 '17 at 14:58
  • My bad, won't happen again – Annemieke Buijs Feb 26 '17 at 15:38
0

Turns out I needed backticks (`) for the variable names like so:

$stmt = $db->prepare("INSERT INTO `pdo-demo` (`firstname`, `lastname`, `email`) value (:firstname, :lastname, :email)");

Now it worked

crystyxn
  • 1,411
  • 4
  • 27
  • 59