Joe Bell
Joe Bell

Reputation: 209

Why is the date entry in PHPMyAdmin is 0000-00-00?

  <?php
$con = mysql_connect("localhost","","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("", $con);

$sql="INSERT INTO PatientData (username, Date, GlucoseReading, Carbohydrates, InsulinUsed, InsulinType,WhichMeal)
VALUES 

('$_Post[username]', '$_POST[Date]', '$_POST[GlucoseReading]', '$_POST[Carbohydrates]', '$_POST[InsulinUsed]','$_POST[InsulinType]', '$_POST[WhichMeal]' )";


if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";
mysql_close($con);
?> 

Upvotes: 1

Views: 10806

Answers (4)

Sudesh
Sudesh

Reputation: 1169

Seeing your post title i believe you have column as date.

In mysql Date field is of format yyyy-mm-dd.

If you try to enter anything else that wont resemble as above formatted date it inserts 0000-00-00.

To rectify it, rather than entering date directly into database format it as below

date('Y-m-d', strtotime($_POST[Date]))

also one more piece of advice seeing your current code, it seems the code is prone to sql injection attack, sanitize data before inserting into db.

Upvotes: 1

Anax
Anax

Reputation: 9372

Adding to what the previous posters have said, make sure your date is valid. For example, 2011-02-31 is an invalid date, which MySQL will turn into 0000-00-00.

Illegal DATETIME, DATE, or TIMESTAMP values are converted to the “zero” value of the appropriate type ('0000-00-00 00:00:00' or '0000-00-00'). (MySQL documentation)

Upvotes: 2

Headshota
Headshota

Reputation: 21439

$_POST["DATE"] is apparently of not correct format or empty.

Upvotes: 2

Shakti Singh
Shakti Singh

Reputation: 86406

Possible reasons are

  1. May be your $_POST[Date] is empty
  2. Format of date is not yyyy-mm-dd mysql accept date in format yyyy-mm-dd

check for these and make sure you must protect your queries against SQL Injection.

Upvotes: 1

Related Questions