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'))."'
Related
$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.
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.
I'm trying to pass a date parameter to a php file so it could insert it into a MySQL table. This is how the date is being transferred:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss%20yyyy-MM-dd");
String currentDateandTime = sdf.format(new Date());
And this is the part in the php file that tuches the date:
$date = $_GET['dt'];
$dateFirst = substr($date, 0, 8);
$dateSecond = substr($date, 11, 10);
$dateComplete = $dateFirst . ' ' . $dateSecond;
$sql=mysql_query("INSERT INTO User
VALUES (NULL, '$uName', '$pass', '$mail', '$disName', 0, '$dateComplete')");
The date field in the table is dateTime. The procedure doesn't fall but the date in the table looks like this: "00:00:00 0000-00-00". Can someone please tell me what am I doing wrong? Thank you in advance!
Switch the order of the concatenation:
$dateComplete = $dateSecond . ' ' . $dateFirst;
The way it is currently written you will be passing '11:07:19 2014-07-09' which is not a date format the MySQL understands.
You can use the mysql function NOW() or just insert the formatted php date
$sql=mysql_query("INSERT INTO User
VALUES (NULL, '$uName', '$pass', '$mail', '$disName', 0, NOW())");
OR
$dateComplete = date('Y-m-d H:i:s');
$sql=mysql_query("INSERT INTO User
VALUES (NULL, '$uName', '$pass', '$mail', '$disName', 0, '$dateComplete')");
I am currently learning OOP, but I can't figure out how to insert date into database.
I have tried:
$time=date('now');
$time=date("timestamp");
$time=date("Y-m-d H:i:s");
$time=date("m/d/y g:i A");
When I submit I get error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '2014-03-11 14:18:07'' at line 1
I can't figure this out. Before I did use:
$result = mysql_query ("INSERT INTO test (title,url,time) VALUES ('$title','$url',now())");
I tried to use now() , but it does not seem to work. :/
Update:
Insert:
public function insert($cat,$title,$article,$author,$image,$time)
{
$sql=mysql_query("INSERT INTO blog_posts(cat, title, article, time, author, image) VALUES('$cat', '$title', '$article', '$author', '$image, '$time'");
if(!$sql)
{
echo mysql_error();
}
}
proccess file:
$cat=$_POST['cat'];
$title=$_POST['title'];
$article= $_POST['article'];
$author=$_POST['author'];
$image=$_POST['image'];
$time= date( 'Y-m-d H:i:s' );
$n=new db();
$n->connect();
$n->insert($cat,$title,$article,$author,$image,$time);
You're missing a quote:
'$author', '$image, '$time'"
^^^^^
HERE
INSERT INTO blog_posts(cat, title, article, time, author, image) VALUES('$cat', '$title', '$article', '$author', '$image', '$time'"
Your parameters are also out of order.
Columns: cat, title, article, time, author, image
Values: '$cat', '$title', '$article', '$author', '$image', '$time'
Forget about the mysql_* functions.
If you want to create future-proof applications you will use PDO. This is OOP.
$db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
$db->exec('SET NAMES utf8'); // Set this transfer's charset to utf-8
$title = 'here is a title';
$url = 'here is a URL';
$time= date( 'Y-m-d H:i:s' ); // I dont know what type the time column is. (date, datetime, timestamp)-> ??
$prepare = $db->prepare("INSERT INTO test (title, url, time) VALUES (:title, :url, :time)");
$prepare->bindParam(':title', $title);
$prepare->bindParam(':url', $url);
$prepare->bindParam(':time', $time);
$prepare->execute(); // Run the query, in this case, insert!
If the time column is of type date or datetime, you can use NOW() or CURDATE() like so:
$prepare = $db->prepare("INSERT INTO test (title, url, time) VALUES (:title, :url, NOW())");
theres nothing to do with objects in this code.
and the first snippet of code is bascily overwriting all over
so its left with the latest. what exactly are you trying to achieve?
could you post some more of your code?
if you wish to use mysql OBJECT oriented, use MySQLi or PDO driver.
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)";