无法将datetime从time()更改为NOW()

时间:2013-08-21 15:25:31

标签: php mysql timestamp

我有一行代码我试图改变......原来的行是插入Unix时间,但我想要CURRENT_TIMESTAMP或NOW。我已将数据库中的字段更改为“datetime”类型。但是,当我更改代码时,我收到以下错误消息:发送邮件时出错。列数与第1行的值计数不匹配

这是原始代码,然后是我试图改变它的方式。任何帮助将不胜感激!

原始代码:

    //if(mysql_query('insert into pm (id, id2, title, user1, user2, message, date_time, user1read, user2read)values("'.$id.'", "'.(intval(mysql_num_rows($req2))+1).'", "", "'.$_SESSION['username'].'", "", "'.$message.'", "'.time().'", "", "")') and mysql_query('update pm set user'.$user_partic.'read="yes" where id="'.$id.'" and id2="1"'))

修改后的代码:

    if(mysql_query('insert into pm (id, id2, title, user1, user2, message, date_time, user1read, user2read)values("'.$id.'", "'.(intval(mysql_num_rows($req2))+1).'", "", "'.$_SESSION['username'].'", "", "'.$message.'", CURRENT_TIMESTAMP())') and mysql_query('update pm set user'.$user_partic.'read="yes" where id="'.$id.'" and id2="1"'))

1 个答案:

答案 0 :(得分:0)

正如错误所说:

您的INSERT声明中有9个项目,但只有7个VALUES。缺少的字段应替换为""和评论中建议的andrewsi。

格式化查询使其更加明显:

INSERT INTO pm (id, id2, title, user1, user2, message, date_time, user1read, user2read)
VALUES("'.$id.'",
       "'.(intval(mysql_num_rows($req2))+1).'",
       "",
       "'.$_SESSION['username'].'",
       "",
       "'.$message.'",
       CURRENT_TIMESTAMP())

正确的查询应该是:

INSERT INTO pm (id, id2, title, user1, user2, message, date_time, user1read, user2read)
VALUES("'.$id.'",
       "'.(intval(mysql_num_rows($req2))+1).'",
       "",
       "'.$_SESSION['username'].'",
       "",
       "'.$message.'",
       CURRENT_TIMESTAMP(),
       "",
       "")

希望这有帮助!