简体   繁体   English

使用PDO将日期插入表格

[英]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. 我有一个PDO插入语句,该语句不插入信息,我对为什么感到困惑。 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 谢谢

@Jim is $name really of type INT ? @Jim是$name真的是INT类型吗?

You can leave it up to PDO to determine the type you know, you don't have to be explicit 您可以将其交给PDO来确定您知道的类型,而不必明确

$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);

You are missing , in the first line after :event . :event之后的第一行中,您不见了。 Maybe that's the problem. 也许这就是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM