-1

I tried inserting a date into a mysql database. but for some reasons it was processing the information before putting it into the database here is my code

define("FRIENDS",  "friends");

if (!$update) { 
     $date = date("m-d-Y");
        $q = "INSERT INTO " . FRIENDS . " VALUES ('$friend2', $date, $status)"; 
    } 

this is what i see in my database: -2025 instead of 15-05-2015. i seems like its being subtracted.

And when i use

$date = date("m/d/Y");

it divides out, leaving 0.000165425971712158 in my database.

Ghostff
  • 1,407
  • 3
  • 18
  • 30

2 Answers2

6

Quote your values.

$q = "INSERT INTO " . FRIENDS . " VALUES ('$friend2', $date, $status)";

ends up looking like

"INSERT INTO tbl_friends VALUES ('friend', 15-05-2015, status)"

mysql does the small equation it finds in your sql query; 15 minus 5, minus 2015

quote your values and it becomes a string instead of an expression.

$q = "INSERT INTO " . FRIENDS . " VALUES ('$friend2', '$date', '$status')";
castis
  • 8,154
  • 4
  • 41
  • 63
2

Your date format is wrong for MySQL storage. Create your date using:

$date = date('Y-m-d');

There is also probably an error in your SQL, unless you have a PHP constant set for "FRIENDS".

EDIT: nevermind, you updated your question showing you have the constant set

Bryan Zwicker
  • 642
  • 1
  • 6
  • 24