Search code examples
phpjquerymysqldatepickerjquery-jtable

can't update and create using datepicker in jquery and jtable


I have got problems with date field. When I create a new record or update an existing one, the date field is saved as 000-00-00.

With the datepicker when I select any day ,It is stored in table with value (0000-00-00).

The DB field is date type.

I download the PHP sample code, create a DB with example sql and change the RecordDate field editable and I have the same problem.

DB value: 0000-00-00 JTable shows: 1899-11-30 What should I do?? Please help This is the jquery code:-

$(document).ready(function () {


   var assignmentid= "<?php echo $assignmentid; ?>";
        //Prepare jTable
        $('#PeopleTableContainer').jtable({
            title: 'Table of people',
            actions: {
                listAction: 'teacherfill.php?action=list&aid="<?php echo $assignmentid; ?>"',
                createAction: 'teacherfill.php?action=create&aid="<?php echo $assignmentid; ?>"',
                updateAction: 'teacherfill.php?action=update&aid="<?php echo $assignmentid; ?>"',
                deleteAction: 'teacherfill.php?action=delete&aid="<?php echo $assignmentid; ?>"'
            },
            fields: {
                lecture_id: {
                    key: true,
                    create: false,
                    edit: false,
                    list: false
                },
                lecture_no: {
                    title: 'Lecture No',
                    width: '40%'
                },
                lecture_date: {
                    title: 'Lecture Date',
                    width: '20%',
                    type:'date'
                }

            }
        });

        //Load person list from server
        $('#PeopleTableContainer').jtable('load');

    });

PHP Code:-

if($_GET["action"] == "update" && $_GET["aid"])
{

$assignmentid=$_GET['aid'];
    $updatedlecno=$_POST["lecture_no"];
    //$updatedlecdate=date("Y-m-d",strtotime($_POST['lecture_date']));
$updatedlecdate=$_POST['lecture_date'];
    $lecid=$_POST["lecture_id"];
    $q="UPDATE lecture_schedule SET lecture_no=$updatedlecno,lecture_date=$updatedlecdate WHERE (lecture_id=$lecid)";
    $result = mysqli_query($conn,$q);
    if($result)
    {
        $er="No error";
    }
else{
    $er1=mysqli_error($conn);
    $er="error";

}
    //Return result to jTable
    $jTableResult = array();
    $jTableResult['Result'] = "OK";
    //$jTableResult['status'] = $er;
    //$jTableResult['date'] = $updatedlecdate;




    print json_encode($jTableResult);
}

Solution

  • Like so, as per my comment on the date format.

      $DateTime = new DateTime($_POST['lecture_date']);
      $lecture_date = $DateTime->format('Y-m-d');
    

    You have to convert it from m/d/Y to the Y-m-d that the database uses ( ISO 8601 )