dongyi0210
dongyi0210
2017-04-17 08:06

在mysql中更新日期

I have a calendar and i want to insert selected date into mysql database using javascript and php but I'm getting 0000-00-00 in my database row

function monthClick(e) {
clickedDays += 1;
$(e).toggleClass("clicked");
startDateIndex = parseInt($(e).attr('id').split('-')[1]);
startDate = new Date(currentYear, currentMonth, startDateIndex);
id = 160;
var urlU = "id=" + id + "&dateC=" + startDate;
$.ajax({
    url : "http://localhost/dateC.php",
    type : "GET",
    cache : false,
    data : urlU,
    success : function(response) {
        alert(response);
    }

});
}

and here is my php file :

    <?php
    $servername = "localhost";
     $username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
 $id=$_GET['id'];
 $dateDebutC=$_GET['dateDebutC'];
// Check connection²M
if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE normal  SET dateC ='$dateC' WHERE id='$id'";
if ($conn->query($sql)) {
       echo 'DB Update successful';
   }
   else {
       echo 'DB Update failed';
   }
$conn->close();
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongyang7152 dongyang7152 4年前

    mysql requires "YY-MM-DD" fromat. Convert your date in "2017-04-17" format before saving into database table, it will work fine.

    点赞 评论 复制链接分享
  • dsk95913 dsk95913 4年前

    You should encode the date before pass it through ajax parameters

    点赞 评论 复制链接分享