Jim
Jim

Reputation: 1315

Inserting a date into a table using PDO

I have a PDO insert statement that is not inserting the information and I am at a loss as to why. Here is my statement:

   $log = $conn->prepare("insert into log_activity (user, event, date) values(:who, :event, :date)");
   $log->bindParam(":who", $name, PDO::PARAM_INT);
   $log->bindParam(":event", $event, PDO::PARAM_STR);
   $log->bindParam(":date", $date, PDO::PARAM_STR);
   $log->execute();

I use a similar one (just no date) for registration that works perfectly. Here is the values:

    $name = $_POST['name'];
    $event = $_POST['thing'];
    $date = $_POST['date'];

I know there are values there, and I am not throwing an error statement. I have tried removing the date thinking that it might be the issue and it still does not work. I am sure I am just missing something easy, but can't find it.

Thanks

Upvotes: 1

Views: 103

Answers (2)

meda
meda

Reputation: 45490

@Jim is $name really of type INT?

You can leave it up to PDO to determine the type you know, you don't have to be explicit

$log = $conn->prepare("insert into log_activity (user, event, date) values(:who, :event, :date)");
$log->bindParam(":who", $name);
$log->bindParam(":event", $event);
$log->bindParam(":date", $date);
$log->execute();

Or just dont bind:

$log = $conn->prepare("insert into log_activity (user, event, date) values(:who, :event, :date)");
$log->execute(array(':who'=>$name, ':event' => $event, ':date'=> $date));

Make sure your connection is set to throw errors:

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Upvotes: 1

m1k1o
m1k1o

Reputation: 2364

You are missing , in the first line after :event. Maybe that's the problem.

Upvotes: 0

Related Questions