简体   繁体   中英

Can't change datetime from time() to NOW ()

I've got a line of code I'm trying to change... the original line was inserting Unix time, but I want CURRENT_TIMESTAMP or NOW. I've changed the field in the database to "datetime" type. However, when I change the code, I get the following error message: An error occurred while sending the message. Column count doesn't match value count at row 1

Here is the original code, followed by how I'm attempting to change it. Any help would be greatly appreciated!

Original code:

    //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"'))

Modified Code:

    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"'))

As the error says:

You have 9 items in your INSERT statement, but only 7 VALUES . The missing fields should be replaced with "" as andrewsi suggested in the comments.

Formatting the query makes it more evident:

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())

The correct query should be:

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(),
       "",
       "")

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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