I have what is most likely a very simple question.. I am designing a simple blogging system and I am trying to put the current date into the table where the blog post is stored whilst waiting for administrator approval. but the method I have used puts 0000-00-00 into the date column! What I am using is as follows:
$query = "INSERT INTO blogentry VALUES ('".$mnam."','".date('d-m-Y h:m:s') ."\n"."','".$mcom."','".$approve."')";
I am relatively new to php so stumble accross errors like this all the time… but I cant seem to google this one!
Thanks guys!
So the easiest way to do this is just let MySQL handle it with the
NOW()function:Another option is to use
TIMESTAMPs by changing your table – set the column to typeTIMESTAMPwithDEFAULT CURRENT_TIMESTAMP, and you can just ignore that column when inserting – it will automatically be filled with the current time. You will need to specify the columns you’re inserting to in order to skip a column:Finally, you NEED to sanitize your inputs. Preferably using prepared statements and PDO (http://php.net/manual/en/pdo.prepared-statements.php), or at least using
mysql_real_escape_string.