calendar我正在使用jQuery创建日历,当我提交日期时,它会存储在我的SQL表中但作为空白结果,例如而不是'09 -07-2017'它显示'00 -00-0000',有人可以帮助我吗?这是我的代码:
Index.PHP:
<!doctype html>
<html lang="en">
<head>
<section class="main-container">
<div class="main-wrapper">
<form class="calendar-form" action="button.inc.php" method="POST">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#datepicker" ).datepicker({
dateFormat: "yy-mm-dd",
} );
} );
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker"></p>
<button type="submit" name="submit">Check Availability</button>
</body>
</html>
Button.Inc.PHP:
<?php
if (isset($_POST['submit'])) {
include_once 'database.inc.php';
$date = mysqli_real_escape_string($conn, $_POST['datepicker']);
$sql = "INSERT INTO system (date) VALUES ('$date');";
$result = mysqli_query($conn, $sql);
}
?>
Dbh.Inc.PHP:
<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "calendar";
$conn = mysqli_connect ($dbServername,$dbUsername,$dbPassword,$dbName);
// Create connection
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
我不明白为什么它不起作用,SQL和jQuery的格式都是一样的但是由于某种原因SQL不会正确存储日期。
这是输出:
答案 0 :(得分:0)
MySQL中的默认日期格式是yyyy-mm-dd
,而不是dd-mm-yy
,这是您在jQuery DatePicker中使用的。您可以在将日期发送到数据库之前格式化日期,也可以使用DatePicker的altField
功能提供如下替代格式:
<script>
$( function() {
$( "#datepicker" ).datepicker({
dateFormat: "dd-mm-yy",
altFormat: "yy-mm-dd",
altField: "#mysql_date",
});
});
</script>
<form>
<input type="hidden" id="mysql_date" name="mysql_date" />
<p>Date: <input type="text" id="datepicker" name="datepicker" /></p>
<button type="submit" name="submit">Check Availability</button>
</form>
注意:我添加了<form>
,因为我在代码中看到了它,并且我还在name
框中添加了input
属性,因为它需要适当的表格提交。