2

I have a form that is trying to insert some data into an SQL Server 2008 database. The form has a function to get the current date/time and then insert it into the database as follows;

$now = date("Y-m-d H:i:s");
$q = "INSERT INTO ".TBL_USERS." ( username, password, userid, userlevel, email, created, updated, timestamp, fullname, avatar )
VALUES ( '$username', '$password', '0', $ulevel, '$email', '$now', '$now', $time, '$fullname', $avatar)";

However, when the form submits it reports an error of;

Warning: mssql_query() [function.mssql-query]: message: Cannot insert the value NULL into column 'created', table 'dbo.users'; column does not allow nulls. INSERT fails.

We have done an echo $q to show the data trying to be inserted and it does show the correct datetime (e.g. 2009-10-28 15:43:00.000), the .000 gets added by the db normally.

If I manually create a record in the database, the datetime in the example above is accepted.

Wondered if anyone had come across this issue before?

Thank you. Neil

1
  • For heaven's sake, us parameterized queries Commented Oct 28, 2009 at 16:04

4 Answers 4

1

Try CURRENT_TIMESTAMP instead of '$now' like so:

$q = "INSERT INTO ".TBL_USERS." ( username, password, userid, userlevel, email, created, updated,   timestamp, fullname, avatar )
VALUES ( '$username', '$password', '0', $ulevel, '$email', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, $time, '$fullname', $avatar)";
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man, CURRENT_TIMESTAMP works perfectly. It's weird how it worked when we were using a different sql server (albeit using the same version and the exact same database).
1
Try NOW() if you are trying to add current timestamp.

    $q = "INSERT INTO ".TBL_USERS." ( username, password, userid, userlevel, 
email, created, updated, timestamp, fullname, avatar )
    VALUES ( '$username', '$password', '0', $ulevel, 
'$email', NOW(), NOW(), $time, '$fullname', $avatar)";

UPDATE

oh its sql server

probably you'd use the CURRENT_TIMESTAMP then

SELECT SYSDATETIME()
    ,SYSDATETIMEOFFSET()
    ,SYSUTCDATETIME()
    ,CURRENT_TIMESTAMP
    ,GETDATE()
    ,GETUTCDATE();
/* Returned:
SYSDATETIME()      2007-04-30 13:10:02.0474381
SYSDATETIMEOFFSET()2007-04-30 13:10:02.0474381 -07:00
SYSUTCDATETIME()   2007-04-30 20:10:02.0474381
CURRENT_TIMESTAMP  2007-04-30 13:10:02.047
GETDATE()          2007-04-30 13:10:02.047
GETUTCDATE()       2007-04-30 20:10:02.047

you can see that current_timestamp gives back date alongwith time.

Comments

1

I had the same problem, possible this solution will be helpful: check that you have a yyyy-DD-mm date format (not yyyy-mm-DD), because by default, mssql accept date in yyyy-DD-mm format.

Comments

0

MSSQL Server has GETDATE() function for current datetime, so try:

$q = "INSERT INTO ".TBL_USERS." ( username, password, userid, userlevel, email, created, updated, timestamp, fullname, avatar )
VALUES ( '$username', '$password', '0', $ulevel, '$email', GETDATE(), GETDATE(), $time, '$fullname', $avatar)";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.