我尝试了我在这里阅读的所有建议,但仍然保存为00-0000-00我希望有人会帮助我。下面的代码将显示htm和php。最后一部分是php。任何帮助都将受到高度赞赏
我的代码是:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "#my_date" ).datepicker({
dateFormat: "MM/dd/yy",
defaultDate: "January/01/1970",
minDate: "January/01/1925",
maxDate: "December/31/2011",
changeMonth: true,
changeYear: true,
yearRange: "1925:2011",
onClose: function(dateText, inst) {
var validDate = $.datepicker.formatDate( "MM/dd/yy", $('#my_date').datepicker('getDate'));
$('#my_date').datepicker('setDate', validDate);
}
});
});
</script>
</head>
<body>
<form action="" method="post" id="myform">
<input name="my_date" id="my_date" type="text" value="January/01/2016" />
<tr><td><label>Add New Election</label></td>
<td><input type="text" name="idelection" value="" style="width:250px; height:34px; border:1px solid #336666;"/></td>
<td><input type="text" name="electitle" value="" style="width:250px; height:34px; border:1px solid #336666;"/></td>
</tr>
<tr>
<td align="center"><input type="submit" value="ADD" name="submit"/></td>
</tr>
</form>
</body>
</html>
** php部分**
<?php
include "connection.php";
session_start();
$con = db();
if(isset($_POST["submit"])){
$id = isset($_POST['idelection']) ? $_POST['idelection'] : '';
$title = isset($_POST['electitle']) ? $_POST['electitle'] : '';
$date1 = $_POST['my_date'];
$date = mysqli_real_escape_string($con,$date1);
$new_date = date('Y-m-d',strtotime($date));
$sql = 'INSERT INTO election_time (elec_id,elec_date) VALUES ("'.$_POST['idelection'].'","$new_date")';
$sql2 = 'INSERT INTO election (elec_id,elec_title) VALUES ("'.$_POST['idelection'].'","'.$_POST['electitle'].'")';
$result = mysqli_query($con,$sql);
$result2 = mysqli_query($con,$sql2);
if(!$result && !$result2){
die("Error on mysql query".mysqli_error());
}
}
?>
答案 0 :(得分:4)
如果您打扰进行任何类型的基本调试:
php > var_dump(strtotime('January/01/1970'));
bool(false)
您的日期格式不是strtotime()
识别的内容,因此它失败,返回布尔值FALSE
,然后您盲目地插入date()
,在此处将其视为整数{{ 1}},所以你的日期实际上是Unix时代0
。
请注意,您的代码可以sql injection attacks开放。
答案 1 :(得分:0)
在你的JS中做一件事。如果可能,请将日期格式更改为此
.