Я пытаюсь сохранить даты, сгенерированные из jQuery datepicker, в базу данных mysql. Я использую ajax, но мне удалось сохранить только 1969-12-31. Поля mysql ожидают значение даты.Вставка даты в базу данных mysql из jQuery
Любая помощь будет большой, спасибо.
DatePicker и форма
<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>
РНР
<?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!"; }
?>
[Ваш скрипт подвержен риску инъекций SQL.] (Http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) –
Каков тип ваше поле даты? –
Что означает 'start' и' end' transfer as, unix timestamp? Также «Ошибка произошла!» Не является хорошим сообщением для отладки, получить реальное сообщение об ошибке. http://php.net/manual/en/mysqli.error.php – chris85