I am using the code below to insert time into a table, and this is the output it's giving me: 0000-00-00

//table
start DATETIME,

//code to insert date into db
$_POST['start']=date("Y-m-d H:i:s");
$insert = "INSERT INTO eguardIPs (Field1,Field2,Field3,start)
VALUES ('".$_POST['Field1']."', '".$_POST['Field2']."', '".$_POST['Field3']."', '".$_POST['start']."')";
$add_start = mysql_query($insert);

Recommended Answers

All 9 Replies

If you want to insert the current timestamp you can use NOW(), like this;

$insert = "INSERT INTO eguardIPs (Field1,Field2,Field3,start)
VALUES ('".$_POST['Field1']."', '".$_POST['Field2']."', '".$_POST['Field3']."', NOW())";

Very well, this does work and I can make do with it for now, though I would hsve liked to save something like this (2010-11-06 15:38:28) in the table, && ! this (2010-11-06). My field is DATETIME.

$insert = "INSERT INTO eguardIPs (Field1,Field2,Field3,start)
 VALUES ('".$_POST['Field1']."', '".$_POST['Field2']."', '".$_POST['Field3']."', NOW())";

Try this.

$date = date("Y-m-d H:i:s"); 
INSERT INTO equardIPs (start) VALUES(" . $date . ")

fobos, that's more or less where I started from, only this time the field is blank!

i forgot to add the ' ' to it in the values

Nothing above seems to work, so I've crafted an ingenious way around it.

When you insert a new record, this does not work

$insert = "INSERT INTO eguardIPs (Field1,Field2,Field3,start)
   
      VALUES ('".$_POST['Field1']."', '".$_POST['Field2']."', '".$_POST['Field3']."', NOW())";

But when you are updating an existing record, it does work

$query = "UPDATE eguardIPs SET start=(now()) WHERE id='".$_POST['id']."'";
	$add_start = mysql_query($query);

So I've decided to first enter data for the other fields, then UPDATE the date field.

$insert = "INSERT INTO eguardIPs (Field1,Field2,Field3)
VALUES ('".$_POST['Field1']."', '".$_POST['Field2']."', '".$_POST['Field3']."')";
$add_member = mysql_query($insert);
						
$check=mysql_query("SELECT * FROM eguardIPs WHERE user!='IloveBibiana'");
						
$_POST['id']=((mysql_num_rows($check))+1);
setcookie(id, $_POST['id']);
						
$_POST['id']=$_COOKIE['id']; 
$query = "UPDATE eguardIPs SET start=(now()) WHERE id='".$_POST['id']."'";
$add_start = mysql_query($query);

A more straightfoward way is still welcome.

Do you get an error if you do it my way ?

$add_member = mysql_query($insert) or die(mysql_error());

Yeah. That way it gives 0000-00-00 00:00:00

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.