Error when trying to insert date into datetime column - php

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

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

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.

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.

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

Related

How can i solve Error with insert into in php

$query = "Insert into $sql_table (date, Firstname, Lastname, StudentID, Score) values ('$date', '$Firstname', '$Lastname' , '$StudentID' , '$mark')";
// execute the query
$result = mysqli_query($conn, $query);
if(!$result)
{
echo "<p>Error with " , $query , "</p>";
}
else
{
echo "<p>Table updated Successfully</p>";
}
Error with Insert into attempts (date, Firstname, Lastname, StudentID, Score) values ('2018-10-21 10:30:21pm', 'jhjsdhfhje', 'bnsdb' , '1023456789' , '6')
Probably going to be that first parameter, but would depend on your table structure.
If date is a date column then MySQL will be expecting a "Y-m-d" formatted string, if it's a datetime then it will be expecting a "Y-m-d H:i:s".
My suggestion would be to drop the "pm" and see if that works.

How do you add date and time into database when submitting a form? [duplicate]

This question already has an answer here:
Mysql saved my date column as 0000-00-00?
(1 answer)
Closed 6 years ago.
I am trying to add date and time when an account is created when submitting the registration form. I am trying this and it delivers: 0000-00-00 00:00:00
$sql = mysql_query("INSERT INTO users (id, username, email, password, created) VALUES (NULL, '$username', '$email', '$password', now())") or die(mysql_error());
This is completely different than the duplicate answered question you are replacing it with..
$date = date('Y-m-d H:i:s');
$sql = mysql_query("INSERT INTO users (id, username, email, password, created) VALUES (NULL, '$username', '$email', '$password', '$date')") or die(mysql_error());
Is it your fields DATETIME OR TIMESTAMP?, be sure that it is one of them.
Also, if your field is TIMESTAMP you can set the default value as CURRENT_TIMESTAMP
Before using now() function, make sure you have set your timezone and check by echoing wat it is printing before putting it into query directly.
//insert raw time stamp into database
$sql = mysql_query("INSERT INTO users (id, username, email, password, created) VALUES (NULL, '$username', '$email', '$password', '" . time() . "')") or die(mysql_error());
//get time stamp from database and format it
$user = mysql_query("SELECT * FROM users where id = '1'");
if (mysql_num_rows($user)>0) {
$row = mysql_fetch_array($user);
echo 'Created: ', date('F jS Y - g:i a', $row['created']);
} else {
echo 'User doesn\'t exist';
}
Take a look into PHP's date function, it takes 2 parameters. First being the format you want, second being the UNIX time stamp.
So if you enter the raw time stamp into the database, you can retrieve it as that and format it how you like when retrieving it.
On another note, take a look into PDO. mysql_* functions will soon be deprecated.

Why the SQL query is not working after addition of two new fields to the query?

I've following SQL query which is working absolutely fine .
$sql = "INSERT INTO user_login (user_id, email, password, token)
VALUES ($user_id, '$email', '$user_password', '$token')";
Now I've added two new fields to the respective table called 'token_creation_time' and 'token_last_accessed_time'. These fields will contain the current time values(i.e. UNIX Time stamp values). For this purpose I used time() function from PHP in the SQL query as follows but it didn't work. I don't know where I'm making a mistake.
$sql = "INSERT INTO user_login (user_id, email, password, token, token_creation_time, token_last_accessed_time)
VALUES ($user_id, '$email', '$user_password', '$token', time(), '')";
The last two field's structure in MySQL DB is as follows :
Name : token_creation_time
Type : int(10)
Collation :
Attributes :
Null : No
Default : None
Extra :
Name : token_last_accessed_time
Type : int(10)
Collation :
Attributes :
Null : No
Default : None
Extra :
In my query I want to insert the current time stamp value only into the field 'token_creation_time'.
Thanks.
Just change the Null:No to Null:Yes,it should work
Name : token_last_accessed_time
Type : int(10)
Collation :
Attributes :
Null : Yes
Default : None
Extra :
I had encountered same error in postgresql when i try to insert an empty string '' for timestamps, so i use nullif function to overcome this:
NULLIF(?, '')
i think mysql has same function
AND make sure your column is nullable
You cannot use php time() function inside query.
Either concatenate this:
$sql = "INSERT INTO user_login (user_id, email, password, token, token_creation_time, token_last_accessed_time)
VALUES ($user_id, '$email', '$user_password', '$token', ".time().", '')";
Or use mysql function UNIX_TIMESTAMP():
$sql = "INSERT INTO user_login (user_id, email, password, token, token_creation_time, token_last_accessed_time)
VALUES ($user_id, '$email', '$user_password', '$token', UNIX_TIMESTAMP(),
'')";
You're inserting a literal string containing time() there.
$sql = "INSERT INTO user_login (user_id, email, password, token,
token_creation_time, token_last_accessed_time)
VALUES ($user_id, '$email', '$user_password', '$token', time(), '')";
error here ^^^^^^
either a) use string concatenation to insert the actual timestamp.
$sql = "INSERT INTO user_login (user_id, email, password, token,
token_creation_time, token_last_accessed_time)
VALUES ($user_id, '$email', '$user_password', '$token', " . time() . ", '')";
b) use pdo with a prepared statement (strongly preferred no matter what) to avoid sql injection attacks, or c) use mysql UNIX_TIMESTAMP() function instead of php time.
$sql = "INSERT INTO user_login (user_id, email, password, token,
token_creation_time, token_last_accessed_time)
VALUES ($user_id, '$email', '$user_password', '$token', UNIX_TIMESTAMP(), '')";
a good way to debug this is to print the query statement out before you execute it, and then run it against the database directly, it will no doubt give you an error above shoving a string into an int or an unknown column
Change your table structure and set NULL attribute to Yes. It will work fine :)
Use now() replace with time() it may work.

INSERT query of xampp MySQL doesn't work

This php insert query is not working in MYSQL xampp. I couldn't find any error
QUERY:
$query = "INSERT INTO member (id, username,fname,lname,email, password, salt )
VALUES ( '$username', '$password', '$email', '$salt' )";
you are missing $fname, $lname in query also use NULL for id if auto incremented
$query = "INSERT INTO member (id, username,fname,lname,email, password, salt )
VALUES (NULL, '$username', '$fname', '$lname', '$password', '$email', '$salt' )";
you are passing wrong number of column names.your correct query should look like this:
$query = "INSERT INTO member (username,password,email,salt )
VALUES ( '$username', '$password', '$email', '$salt' )";
You are not inserting values to all the columns specified in the query.
In your query
$query = "INSERT INTO member (id, username,fname,lname,email, password, salt )
VALUES ( '$username', '$password', '$email', '$salt' )";
You are specifying 7 columns and only 4 values .So either add more values or remove unnecessary columns specified in the query like
$query = "INSERT INTO member (username, password,email, salt )
VALUES ( '$username', '$password', '$email', '$salt' )";
OR
$query = "INSERT INTO member (id, username,fname,lname,email, password, salt )
VALUES ('$id', '$username','$fname','$lname','$email', '$password', '$salt' )";

Working with strtotime and MySQL

This works...
mysqli_query
($con, "
INSERT INTO myDB (id, name, mydate)
VALUES ('$id', '$name', now())
")
This doesn't work....
mysqli_query
($con, "
INSERT INTO myDB (id, name, mydate)
VALUES ('$id', '$name', date('Y-m-d H:i:s', strtotime('+5 hour')))
")
The mydate column in MySQL is of datetime format.
Any ideas why it's not working? I'm currently using now(), but I want it to show the time in my timezone, not the server's timezone.
I'd suggest storing the date in a variable first.
$date = date('Y-m-d H:i:s', strtotime('+5 hour'));
If both your MySQL and PHP servers are operating on the same time-zone and have their clocks properly synchronized, you wont have an issue.
and then execute the query like so:
mysqli_query($con, "
INSERT INTO myDB (id, name, mydate)
VALUES ('$id', '$name', '$date')
")
I hope this helps!
strtotime() is not MySQL function, its PHP function, you need to write it for being executes...
"INSERT INTO myDB (id, name, mydate)
VALUES ('$id', '$name', '".date('Y-m-d H:i:s', strtotime('+5 hour'))."'

Categories