PHP的日期插入到我的数据库后始终是1970-01-01和2018-08-31

时间:2018-08-19 06:10:59

标签: php date mysqli

美好的一天!我试图将日期插入到mysql表中。我创建的表如下:Table

我要使用以下函数语句插入数据:

if (isset($_POST['setSchedule'])) {
            $data->set_Schedule($test, $conn);
            echo"<script>location.replace('EvaluationSchedule.php');</script>";
        }

我的sql插入函数插入dateFrom和dateTo:

function set_Schedule($test, $conn){
            $setSchedule = date('Y-m-d',strtotime($_POST['setSchedule']));
            $lastSchedule = date('Y-m-d',strtotime($_POST['lastSchedule']));

            $studentLimit = $_POST['studentLimit'];

            $q = "INSERT INTO evaluation VALUES(null, ?,?,?) ";
            $stmt = $conn->prepare($q);
            $stmt->bind_param("ssi", $setSchedule, $lastSchedule,$studentLimit);
            $stmt->execute();
        }

当我插入日期值之后,始终是1970-01-01和2018-08-31: Table values

这是我的表单代码:

<form class="form-horizontal" method="POST">
                <div class="form-group">
                            <label class="control-label col-md-8">Set Evaluation:
                                <input class="form-control col-sm-8" type="date" id="datepicker" placeholder="Enter Student ID" name="setSchedule" autocomplete="off">
                            </label>
                        </div>
                        <div class="form-group ">
                            <label class="control-label col-md-8">Last Evaluation:
                                <input class="form-control col-sm-8" type="date" id="datepicker1" placeholder="Enter Student ID" name="lastSchedule" autocomplete="off">
                            </label>
                        </div>

                        <div class="form-group ">
                            <label class="control-label col-md-8">Student limit:
                                <input class="form-control col-sm-8" type="text"  placeholder="Enter Student ID" name="studentLimit" autocomplete="off">
                            </label>
                        </div>

                        <div class="row">
                            <div class="col-md-8 col-md-offset-3">
                                <button class="btn btn-primary" type="submit" name="setSchedule"   id="demoNotify""><i class="fa fa-fw fa-lg fa-check-circle"></i>Save</button>&nbsp;&nbsp;&nbsp;<a class="btn btn-secondary" href="EvaluationSchedule.php"><i class="fa fa-fw fa-lg fa-times-circle"></i>Back</a>
                            </div>
                        </div>
                    </div>
                </form>

我是SQL的新手,现在开始学习。谁能给我一些启示。.我做错了什么?

2 个答案:

答案 0 :(得分:2)

问题在于,您在“提交”按钮上使用的是name="setSchedule",因此它将覆盖您的第一个日期字段中的那个。并导致无效的日期。

检查$_POST的值将显示此内容。

答案 1 :(得分:-1)

Your insert statement is missing some stuff

$q = "INSERT INTO evaluation VALUES(null, ?,?,?) ";

Field names are missing

$q = "INSERT INTO evaluation (field1,field2,field3, field4) VALUES(null, ?,?,?) ";

The values you are getting are maybe the default values.