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

jquery mysql calendar

83 观看

1回复

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:

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

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:

database

mysql

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

回应 1


0

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:

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

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 发布者: 2017 年 12 月 27 日
32x32