0

I am trying to save dates generated from jQuery datepicker into a mysql database. I am using ajax but I only ever manage to save 1969-12-31. The mysql fields are expecting a date value.

Any help would be great, many thanks.

The datepicker and form

<script>
$(document).ready(function(){
$("#success").hide();
 $(function() {
    $( "#start" ).datepicker({

      defaultDate: "+1w",
      changeMonth: true,
      numberOfMonths: 3,
      onClose: function( selectedDate ) {
        $( "#end" ).datepicker( "option", "minDate", selectedDate );
      }
    });
    $( "#end" ).datepicker({
      defaultDate: "+1w",

      changeMonth: true,
      numberOfMonths: 3,
      onClose: function( selectedDate ) {
        $( "#start" ).datepicker( "option", "maxDate", selectedDate );
      }
    });
  });
  $("#submitMe").on("click",function(){
    var formdata = $(this.form).serialize();
    $.post('dateinsert.php', formdata,
           function(data){
$("#message").html(data);
$("#success").hide();
$("#success").fadeIn(500); //Fade in  
});
return false;
  });
});
  </script>
</head>
<body>
<div class="container">
 <div id="success" /></div>
</br>
  <form> 
<label for="start">From</label>
<input type="text" id="start" name="start">
<label for="end">to</label>
<input type="text" id="end" name="end">
  <button type="submit" id="submitMe" class="btn btn-primary">Submit</button>
</form>

The PHP

<?php
//include db configuration file
include_once("config.php");


 $start=$_POST['start'];
 $end=$_POST['end'];
 $starttime = date ("Y-m-d", $start);
 $endtime = date ("Y-m-d", $end);

 //Insert Data into mysql
$insert_row = $mysqli->query("INSERT INTO dates(start,end) VALUES ('$starttime','$endtime')"); 
if($insert_row){
echo 
         "success";
}
else{ echo "An error occurred!"; }
?>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
JulianJ
  • 1,259
  • 3
  • 22
  • 52
  • 2
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Jan 06 '16 at 20:55
  • what is the type of your date field? – Gouda Elalfy Jan 06 '16 at 20:57
  • What do `start` and `end` transfer as, unix timestamp? Also `An error occurred!` isn't a good message to debug with, get the real error message. http://php.net/manual/en/mysqli.error.php – chris85 Jan 06 '16 at 20:58
  • date ("Y-m-d", strtotime ($start)); – devpro Jan 06 '16 at 20:59
  • Is your form making a POST or GET request because it's not specified in the HTML? What format is the date string created by Javascript? – RCrowt Jan 06 '16 at 21:07

1 Answers1

4

The PHP date function expects the 2nd parameter to be a UNIX style date. The default format from datepicker is mm/dd/yy which means that when you call date you are passing it an invalid value and the result is the earliest date that the date function can deal with.

Try using:

$starttime = date ("Y-m-d", strtotime($start));
$endtime = date ("Y-m-d", strtotime($end));

And do heed the comment about SQL injection.

Dave
  • 5,108
  • 16
  • 30
  • 40