views:

57

answers:

4

Hello i want to send to a MySQL data base a date and time in a format compatible with the mysql DateTime format, wich is: 0000-00-00 00:00:00 ...

Im using this code who brings the date and time from a PHP command:

   $insert = "INSERT INTO sms (ref, texto, fecha)
   VALUES ('".$_POST['usuario']."', '".$_POST['sms']."', '".date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000))."')";
   $add_member = mysql_query($insert);

I really want to do this but with a MySQL command, i mean, using the Date and Time from the mysql server. Thanks

+1  A: 

Use NOW() when inserting the record INSERT INTO table (date) VALUES (NOW())

For your code:

$insert = "INSERT INTO sms (ref, texto, fecha)
VALUES ('".$_POST['usuario']."', '".$_POST['sms']."', NOW() )";
$add_member = mysql_query($insert);
webdestroya
can you please add that in my code? $insert = "INSERT INTO sms (ref, texto, fecha) VALUES ('".$_POST['usuario']."', '".$_POST['sms']."', '".date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000))."')"; $add_member = mysql_query($insert);
DomingoSL
@DomingoSL - Ok, I updated my post. My code posts the current time on the MySQL server. It does not post july 1st 2000. If you are going to do that, you are better off just doing '2000-07-01'
webdestroya
for some reason it does not work
DomingoSL
@DomingoSL - Is `fecha` a `DATETIME` type? Other than that... I know the code works, I use it in many of my sites. So I'm not sure what is wrong. What error are you getting?
webdestroya
I solved, i really dont know how, maybe you can help me to understand what i did, i change the code to:$insert = "INSERT INTO sms (ref, texto, fecha) VALUES ('" . addslashes($_POST['usuario']) . "', '" . addslashes($_POST['sms']) . "', NOW() )";And it works just fine!
DomingoSL
@DomingoSL - Nothing, at least nothing pertaining to the question. You just added santization to your query (which you should have done from the beginning).
webdestroya
Don't use `addslashes()`; it escapes some characters wrong. The correct function is `mysql_real_escape_string()`.
staticsan
`NOW()` is **not** a good replacement for `date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000))`. I mean, it doesn't happen very often that `NOW()` is exactly "July 1st 2000 00:00:00".
Alexander Konstantinov
I think he wanted to add the current date, and was just using that as an example.
webdestroya
+1  A: 

MySQL allows only limited number of date formats provided as an input. From MySQL manual:

Only the formats described in the following sections are supported. It is expected that you supply legal values. Unpredictable results may occur if you use values in other formats.

You're providing date in the format Y-m-d\TH:i:sP (value of the DATE_ATOM constant), which is not supported by MySQL (though it may be parsed and stored correctly depending on the current SQL mode).

You should use date('Y-m-d H:i:s', ...) instead.

P.S. And, yes, you should protect your code from SQL injections.

Alexander Konstantinov
A: 
$date = date("Y-m-d H:i:s", mktime(0, 0, 0, 7, 1, 2000));

$usuario = mysql_real_escape_string($_POST['usuario']);
$sms = mysql_real_escape_string($_POST['sms']);
$date = mysql_real_escape_string($date);

$sql = "INSERT INTO sms (ref, texto, fecha) VALUES ('$usuario', '$sms', '$date')";
mysql_query($sql) or trigger_error(mysql_error().$sql);
Col. Shrapnel
A: 

First I want to tell you that you should follow the advice in the comments and read up on SQL injection.

This code will insert the record with the current time in the right format.

$insert = "INSERT INTO sms (ref, texto, fecha)
VALUES ('".$_POST['usuario']."', '".$_POST['sms']."', '".date('Y-m-d H:i:s')."' )";
$add_member = mysql_query($insert);

If you don't want the current time you need to change date('Y-m-d H:i:s') into date('Y-m-d H:i:s', some_unix_timestamp)