Andrew Asmer
Andrew Asmer

Reputation: 43

PHP date and time function wont store in the database

the date in the Database has a field of time which you can insert a date and a time but why is it that it is not being stored all I get is 0's when I check the database, but the other datas are being stored

here's my code

<?php 

session_start(); 
include("Connection.php");
    if (isset($_POST['submit']))  
    $name = $_POST['customerName'];


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

    mysql_query("INSERT INTO  `starbucks`.`orders` (
`ID` ,
`NAME` ,
`TOTAL_PRICE` ,
`TOTAL_ITEMS` ,
`TIME`
)
VALUES (
'' ,  '$name',  '', '','$phpdate')")  or die(mysql_error());

    $_SESSION['user'] = $name; 
    ?>

Upvotes: 0

Views: 1221

Answers (3)

clem
clem

Reputation: 3366

if you are simply trying to add the current time, you don't need to create a timestamp in php but you can use NOW() in mysql instead

mysql_query("INSERT INTO  `starbucks`.`orders` (
`ID` ,
`NAME` ,
`TOTAL_PRICE` ,
`TOTAL_ITEMS` ,
`TIME`
)
VALUES (
'' ,  '$name',  '', '',NOW())") 

Upvotes: 1

axeven
axeven

Reputation: 166

use your $mysqldate instead of $phpdate

mysql_query("INSERT INTO starbucks.orders ( ID , NAME , TOTAL_PRICE , TOTAL_ITEMS , TIME ) VALUES ( '' , '$name', '', '','$mysqldate')") or die(mysql_error());

Upvotes: 1

Kokos
Kokos

Reputation: 9121

DATETIME field is a date field, not a timestamp field.

The format is date('Y-m-d H:i:s').

Upvotes: 2

Related Questions