2017-09-16 66 views
1

我試圖在數據庫中存儲日期選擇器的值。到目前爲止,我已經能夠存儲其他值而不是日期選擇器值。數據庫中的存儲日期選擇器值

形式:

    <form class="form-signin" name="Register_Form" method="post" action="regcheck.php"> 
        <h2 class="form-signin-heading">Please sign in</h2> 

        <label for="SecondName" class="sr-only">Second Name</label> 
        <input type="SecondName" id="SecondName" name="SecondName" class="form-control" placeholder="Second Name" required> 

        <div class="input-group input-append date" id="dateRangePicker"> <!-- Date input --> 
         <label for="Course" class="sr-only">Date</label> 
        <input class="form-control" id="date" name="date" placeholder="MM/DD/YYYY" type="text" /> 
         <span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span> 
        </div> 

        <button class="btn btn-lg btn-primary btn-block" type="reg" name="reg" value="Register">Register</button> 
        </form> 

代碼插入到數據庫:

  require_once 'connect.php'; 
      if (isset($_POST['reg'])){ 
         $dob = $_POST['date']; 
         $Student_ID = $_POST['Student_ID']; 
         $gender = $_POST['gender']; 
         $course = $_POST['Course']; 
         $email = $_POST['inputEmail']; 
         $password = $_POST['inputPassword']; 
         $FN = $_POST['FirstName']; 
         $SN = $_POST['SecondName']; 

        $sql = "INSERT INTO tblaccounts (Email, Password, Student_ID, FirstName, SecondName, Course, Gender, DoB) VALUES ('".$email."','".$password."','".$Student_ID."','".$FN."','".$SN."','".$course."','".$gender."','".$dob."')"; 
        $result = mysqli_query($connection, $sql) or die("Database Connection Failed" . mysqli_error($connection)); 
        //$count = mysqli_num_rows($result); 
        echo "Registeration Successful!:"; 

        header('Location: login.php'); 
      } else { 
         echo "Registeration Failed!:";# 
         ?><br/><a href ="login.php">Go back to the login screen.</a><?php 
        } 
      ?> 
+0

什麼是爲您的'DoB'列設置的數據類型? – Twisty

回答

0

什麼格式是DB中的DOB場? 如果它的時間戳,那麼你需要做的

$dob = strtotime($_POST['date']); 

如果它的日期,那麼你需要做的

$dob = date('Y-m-d', strtotime($_POST['date'])); 

如果它的日期時間,那麼你需要做的

$dob = date('Y-m-d 00:00:00', strtotime($_POST['date'])); 
0

在MySQL日期類型你不能存儲日期像MM/DD/YYYY你必須改變日期格式爲YYYY-MM-DD。改變輸入像

$dob = date('Y-m-d', strtotime($_POST['date'])); 
+0

使用''Y-m-d'',不會導致'YYYY/MM/DD',它會導致'YYYY-MM-DD'。你想使用''Y/m/d''來產生'YYYY/MM/DD'。只是說。 – Twisty

相關問題