Glenn
Glenn

Reputation: 79

Can anyone tell me why I am getting all zeros in my date field

I'm fairly new to php, yes I know about the SQL injection attack, just trying to make this work. Everything posts to the mysql table properly except the date, I keep getting all zeros. Why? The field is set to datetime.

include('../htconfig/dbConfig.php'); 
$dbSuccess = false;
$dbConnected = mysql_connect($db['hostname'],$db['username'],$db['password']);

if ($dbConnected) {     
    $dbSelected = mysql_select_db($db['database'],$dbConnected);
    if ($dbSelected) {
        $dbSuccess = true;
    }   
}
$Sldate = date("m-d-Y");
$data_back = json_decode(file_get_contents('php://input'));


    $userName = $data_back->{"User"};
    $password = $data_back->{"Pword"};
    $SoldTo = $data_back->{"Soldto"};
    $SoldPrice = $data_back->{"SoldPrice"};
    $SoldEmail= $data_back->{"Email"};
    $VIN = $data_back->{"VIN"};



mysql_query("UPDATE tblinventory SET SOLD ='1',DATESLD = '$Sldate', DealerName='$SoldTo', SoldPrice = '$SoldPrice', DealerEmail = '$SoldEmail' WHERE VIN =$VIN");
echo ('SOLD');

///send email

?>

Upvotes: 0

Views: 856

Answers (2)

EternalHour
EternalHour

Reputation: 8621

The reason your are getting 00-00-00 00:00:00 is because MYSQL can't recognize your datetime as valid so it inserts a default value. Sometimes you'll also get 1947-08-25 19:31:00.

Your datetime object will need to be formatted correctly. PHP's DateTime class makes easy work of this, however you need version 5.2 to use it.

$date = new DateTime();
$Sldate = $date->format('Y-m-d');

Upvotes: 0

John Conde
John Conde

Reputation: 219864

MySQL expects your date to be in YYYY-MM-DD format, not MM-DD-YYYY.

$Sldate = date("Y-m-d");

You could also remove the PHP portion of this code and do this directly in your SQL

DATESLD = CURDATE()

Upvotes: 2

Related Questions