4 Replies - 941 Views - Last Post: 04 April 2013 - 07:07 PM Rate Topic: -----

#1 eeveemaria20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-April 13

PHP current datetime insert to mysql how?

Posted 03 April 2013 - 03:42 AM

I have this code
<?php

$date = date('Y-m-d H:i:s'); 

$sql = mysql_query("INSERT INTO reservation (date_reserved) 
VALUES('$date'") or die (mysql_error());

?>



and it returns an error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2

:(/> Please anyone can help me..

This post has been edited by Dormilich: 03 April 2013 - 04:10 AM
Reason for edit:: please use [code] [/code] tags when posting code


Is This A Good Question/Topic? 0
  • +

Replies To: PHP current datetime insert to mysql how?

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3227
  • View blog
  • Posts: 10,837
  • Joined: 12-December 12

Re: PHP current datetime insert to mysql how?

Posted 03 April 2013 - 03:44 AM

$sql = mysql_query("INSERT INTO reservation (date_reserved)
    VALUES('$date')") or die (mysql_error());

Was This Post Helpful? 0
  • +
  • -

#3 creativecoding  Icon User is online

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,204
  • Joined: 19-January 10

Re: PHP current datetime insert to mysql how?

Posted 03 April 2013 - 07:13 PM

You can just use the built in mysql functions.

"INSERT INTO reservation (date_reserved) 
VALUES(NOW())"

This post has been edited by creativecoding: 04 April 2013 - 07:09 PM

Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

Re: PHP current datetime insert to mysql how?

Posted 04 April 2013 - 06:43 PM

Using DATETIME like that won't work, CC.

You probably meant to use the NOW() function or the CURRENT_TIMESTAMP constant/function.
INSERT INTO my_table (date_field)
VALUES (NOW())


CREATE TABLE my_table (
    id INT NOT NULL PRIMARY KEY,
    ts_field TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);



Both of those are equivalent to, and preferable to, creating a date('Y-m-d H:i:s') value in PHP and injecting that into the queries.
Was This Post Helpful? 1
  • +
  • -

#5 creativecoding  Icon User is online

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,204
  • Joined: 19-January 10

Re: PHP current datetime insert to mysql how?

Posted 04 April 2013 - 07:07 PM

Oops, thanks for pointing that out. I edited my original post.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1