1

I'm gathering some info from a website to put it in a MySQLdatabase. At this moment I just cannot find out how to get the date and time in the database.

I tried several things, can you help?

$write="REPLACE INTO `".$database."`.`db`     (`1`,`2`,`3`,`4`,`5`,`6`,`7`,`datetime`) VALUES     ('".$1."','".$2."','".$3."','".$4."','".$5."','".$6."','".$7."','**SO WHAT DO I NEED TO PLACE HERE**')";
echo $write;
$query = mysql_query($write) or die (mysql_error()); 

In the database itself no matter what I put in my php, is 0000-00-00 00:00:00.

Troel
  • 11
  • 2

2 Answers2

1

just passed value NOW(), example

INSERT INTO tb(col1) VALUES(NOW())

As a sidenote, the query is vulnerable with SQL Injection if the value(s) of the variables came from the outside. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • I did try NOW, but it still gives me all those zero's. Thanks for the injection thing, I will look into that as soon as the time is working ;) – Troel May 04 '13 at 18:09
  • are you wrapping `NOW()` with single quotes? what is the data type of your column? – John Woo May 04 '13 at 18:09
  • The column is DATETIME. (don't hit enter for the next line :( ) Without quotes I get an error: NOW does not exist. Check the 'Function Name Parsing and Resolution' section in the Reference Manual – Troel May 04 '13 at 18:13
  • I see it's working there, but there are some differences there and in my code :) I'm not sure I can use that whole thing in my code? – Troel May 04 '13 at 18:18
  • it should be `NOW()` not `NOW` – John Woo May 04 '13 at 18:18
  • IT WORKS! I added a space where it shouldn't be! Thank you guys! – Troel May 04 '13 at 18:22
0

if it's fine to use server's current date/time then use NOW() function

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141