Need help with php/mysql outputting date

Soldato
Joined
1 Dec 2004
Posts
23,076
Location
S.Wales
right iv managed to get it to output something...

Date: 2007-05-04 00:00:00

as you can see its outputted the date that it was submitted, but not the time...

iv created a table called entry_date which has a type of "datetime" and the value " 0000-00-00 00:00:00 "

do i need to insert additional code for the time or should it carry it out using the same process.
Code:
[<?php

include "connect.php";

if(isset($_POST['submit']))

{

   $entry_date=$_POST['entry_date'];

   $name=$_POST['name'];

   $comment=$_POST['comment'];

   
   if(strlen($name)<1)

   {

      print "You did not enter a name.";

   }

   else if(strlen($comment)<1)

   {

      print "You did not enter a comment.";

   }

   else

   {

      $insert="Insert into visitordata (name,comment,entry_date) values('$name','$comment',current_date)";

      mysql_query($insert) or die("Could not insert comment");

      print "Comment added. <A href='display.php'>Click here</a> to see all comments.";

     }

  }

?>
 
i don't know mysql but i'm guessing "current_date" is a mysql function? isn't it kind of obvious from the name the reason why you're not getting the time? :p

a quick google suggests using now() :)

edit: only 7 minutes too slow. :D
 
LazyManc said:

Yes, and correctly capitalize your SQL.
Code:
INSERT INTO `visitordata` ( `name` , `comment` , `entry_date`) VALUES (...

http://uk.php.net/mysql_real_escape_string

Use this function to eliminate SQL injection.

Code:
function quote_smart($value) {
   // Stripslashes
   if (get_magic_quotes_gpc()) {
       $value = stripslashes($value);
   }
   // Quote if not integer
   if (!is_numeric($value) || $value[0] == '0') {
       $value = "'" . mysql_real_escape_string($value) . "'";
   }
   return $value;
}

E.g.

Code:
VALUES ( " . quote_smart($name) . " , " . quote_smart($next_input) . " , ...
 
Last edited:
Back
Top Bottom