0

I am using the below insert to add data to a mysql table. I have created a date field in the db called 'Date'. How can I add todays date to the insert?

$sql = "INSERT INTO queries SET Name='$Name', Email='$Email', Telephone='$Telephone', Country='$Country', Referrer='$Referrer', Website='$Website'";
user1823053
  • 67
  • 1
  • 2
  • 8
  • 4
    `NOW()` in your query. – Martijn Mar 15 '14 at 10:29
  • another solution: you could modify the date row and let it auto-populate with the current date, by using `CURRENT_TIMESTAMP` for `DEFAULT`: http://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html – cweinberger Mar 15 '14 at 10:31
  • Your code is vulnerable to SQL Injection. See [How can I prevent SQL injection in PHP?](http://stackoverflow.com/q/60174) – Madara's Ghost Mar 15 '14 at 10:31
  • @cweinberger I think CURRENT_TIMESTAMP will not work because type is date not time. –  Mar 15 '14 at 10:36
  • Use cur_date() to add today's date. Check http://www.w3schools.com/sql/func_curdate.asp – Gunaseelan Mar 15 '14 at 11:18

3 Answers3

1

Use MySQL now() function

$sql = "INSERT INTO queries SET Name='$Name', Email='$Email', Telephone='$Telephone', Country='$Country', Referrer='$Referrer', Website='$Website', date=NOW()";
Dulitha K
  • 2,088
  • 1
  • 19
  • 18
  • I have tried this and the entry in the db just shows as 0000-00-00 – user1823053 Mar 15 '14 at 10:48
  • I have checked it. It should work. please make sure that you haven't add single quotes for now() function(ex: 'now()'). The reference http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_now – Dulitha K Mar 15 '14 at 12:31
0

by php

$time = time();
$sql = "INSERT INTO queries SET Name='$Name', Email='$Email', Telephone='$Telephone', Country='$Country', Referrer='$Referrer', Website='$Website', `Date` = $time";

i added time by unix , because that's will be easy to make operation in at

Mostafa King
  • 113
  • 2
  • 10
0

you could use CURDATE()

$sql = "INSERT INTO queries SET Name='$Name', Email='$Email', Telephone='$Telephone', Country='$Country', Referrer='$Referrer', Website='$Website', `Date` = CURDATE()";
vimal1083
  • 8,499
  • 6
  • 34
  • 50