SQL date format is showing blank result (using jQuery e.g. 00-00-0000)

jquery mysql calendar

83 观看


0 作者的声誉

calendarI am creating a calendar using jQuery and when I submit the date, it stores in my SQL table but as a blank result e.g. instead of '09-07-2017' it shows '00-00-0000', can someone help me? Here is my code:


    <!doctype html>
    <html lang="en">
    <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>
      $( function() {
        $( "#datepicker" ).datepicker({
        dateFormat: "yy-mm-dd",

        } );
      } );

    <p>Date: <input type="text" id="datepicker"></p>
     <button type="submit" name="submit">Check Availability</button>


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);




$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";

I don't understand why it doesn't work, the formatting of both the SQL and jQuery are the same but for some reason the SQL wont store the date properly.

This is the output:



作者: user9131365 的来源 发布者: 2017 年 12 月 27 日

回应 (1)


21012 作者的声誉


The default date format in MySQL is yyyy-mm-dd, not dd-mm-yy which is what you're using in jQuery DatePicker. You can either format the date before you send it to the database, or you can use the altField feature of the DatePicker to provide the alternative format like this:

    $( function() {
        $( "#datepicker" ).datepicker({
            dateFormat: "dd-mm-yy",
            altFormat: "yy-mm-dd",
            altField: "#mysql_date",

    <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>

Note: I've added a <form> because I see it missing in your code, and I've also added the name attribute to the input boxes because it is needed for proper form submission.

作者: Racil Hilan 发布者: 27.12.2017 05:49