I am trying to put a timestamp on when a new line is added to my database, (for displaying it later on, kind of like a blog).
Here's my code right now, but it won't register the date.
$sql = 'INSERT INTO `Content` (postContent, datePosted) VALUES ("'.(string)$content.'", "'SELECT getdate()'")';
What is the right syntax of getdate() in this situation?
EDIT: this is in php, also the date has to be associated with the "Content".
Thanks.
EDIT2: I am a goof, I could have just put CURRENT_TIMESTAMP in 'default' in MySQL and it would have automatically have done it. Please resort to this if you attempt it yourself instead of spending over 2 hours trying to fix this manually like me. Thank you for your help!
your passing like a string replace it like this.
you just remove the double quotes and single quotes from query
$sql = 'INSERT INTO `Content` (postContent, datePosted) VALUES ("'.(string)$content.'",SELECT getdate())';
Replace this code
$sql = 'INSERT INTO `Content` (postContent, datePosted) VALUES ("'.(string)$content.'",SELECT getdate())';
With this code:
$date = getdate();
$sql = "INSERT INTO `Content` (postContent, datePosted) VALUES ('".$content."', '".$date[0]."')";
The value that will be stored in datePosted has the following format:
Seconds since the Unix Epoch, similar to the values returned by time() and used by date().
What this means for you is that you will be able to easily compare your database value by either creating a $currentDate = getdate()
or even easier by just comparing it to time();
which as the exact same format.
Let me know if that helped!
insert into [table_name] ([column_name]) values (now())