简体   繁体   中英

SQL Type NOW() not working

I have this query;

"INSERT INTO download(download_key, ip, file, expire) VALUES ('".$text."', 
'".$mysql->real_escape_string($_SERVER['REMOTE_ADDR'])."', 
'".$searchQuery['file_match']."', 
'NOW() + INTERVAL 10 MINUTES')"

When I insert this in the database, the row expire is always 0000-00-00 00:00:00. I have tried the types:

  • TIMESTAMP
  • DATE
  • DATETIME
  • TIME

But is always 0000-00-00 00:00:00 or 00:00:00.

What is the right type for NOW()? I`m using MYSQL

删除引号,引号用于字符串

NOW() + INTERVAL 10 MINUTE
"INSERT INTO download(download_key, ip, file, expire) VALUES ('".$text."', 
'".$mysql->real_escape_string($_SERVER['REMOTE_ADDR'])."', 
'".$searchQuery['file_match']."', 
NOW() + INTERVAL 10 MINUTES)"

I'm gonna add a little thing to people already said.

Except the thing you shouldn't pass sql functions as string so don't put NOW() + INTERVAL 10 MINUTES in quotes, you can pass a string for date also. You can generate this date with php for example.

Try this fiddle to check how to put simple DATE in database: http://sqlfiddle.com/#!2/cddc1/1

Also with PHP you can generate the date with this code:

$minutes = 10;
$date = date('Y-m-d H:i:s', time() + $minutes*60);
$query = "INSERT INTO download(download_key, ip, file, expire) VALUES ('$text', 
'{$mysql->real_escape_string($_SERVER['REMOTE_ADDR'])}', 
'{$searchQuery['file_match']}', 
'$date')";

感谢@Kelu Thatsall:修复:

NOW() + INTERVAL 10 MINUTE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM