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!"; }
?>
5
  • 2
    Your script is at risk for SQL Injection Attacks. Commented Jan 6, 2016 at 20:55
  • what is the type of your date field? Commented Jan 6, 2016 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. php.net/manual/en/mysqli.error.php Commented Jan 6, 2016 at 20:58
  • date ("Y-m-d", strtotime ($start)); Commented Jan 6, 2016 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? Commented Jan 6, 2016 at 21:07

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That was great and works perfectly and point taken about SQL injection. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.