DOB mysql插入

时间:2017-05-17 13:43:16

标签: php html mysql pdo

我尝试将数据从下拉列表(日月)插入到我的数据库中但是我遇到了错误,请帮忙。 DOB 在我的数据库中设置为日期格式。

PS。但是,它确实插入我的 没有任何问题。

提前致谢并抱歉英语能力差=(

以下是我的PHP代码

<?php

require '../ppuyakul/php/db_conn.php';

error_reporting(0);
$message = '';
$year = $_POST['year'];
$month = $_POST['month'];
$date = $_POST['date'];
$DOB = date("Y-m-d", mktime(0,0,0,$month, $day, $year));



if(!empty($_POST['email']) && !empty($_POST['password']) && !empty($_POST['fullname']) && !empty($_POST['username']) && !empty($_POST['password_confirmation']) && !empty($_POST['gender']) && !empty($_POST['country']) && !empty($_POST['state']) && !empty($_POST['city']) && !empty($_POST['day']) && !empty($_POST['month']) && !empty($_POST['year'])):

  // Enter the new user in the database
  $sql = "INSERT INTO assignment2 (fullname,username, email, password, passwordcon, gender, country, state, city, day, month, year, DOB) VALUES (:fullname, :username, :email, :password, :password_confirmation, :gender, :country, :state, :city, :day, :month, :year, DOB)";
  $stmt = $conn->prepare($sql);

  $stmt->bindParam(':fullname', $_POST['fullname']);
  $stmt->bindParam(':username', $_POST['username']);
  $stmt->bindParam(':email', $_POST['email']);
  $stmt->bindParam(':password', password_hash($_POST['password'], PASSWORD_BCRYPT));
  $stmt->bindParam(':password_confirmation', password_hash($_POST['password_confirmation'], PASSWORD_BCRYPT));
  $stmt->bindParam(':gender', $_POST['gender']);
  $stmt->bindParam(':country', $_POST['country']);
  $stmt->bindParam(':state', $_POST['state']);
  $stmt->bindParam(':city', $_POST['city']);
  $stmt->bindParam(':day', $_POST['day']);
  $stmt->bindParam(':month', $_POST['month']);
  $stmt->bindParam(':year', $_POST['year']);
  $stmt->bindParam(':DOB', $_POST['year'], $_POST['month'], $_POST['day']);


  if( $stmt->execute() ):
    $message = 'Successfully created new user';
  else:
    $message = 'Sorry there must have been an issue creating your account';
  endif;
endif;

?>

这是html中的Dropdown代码

<div>
           <p style="display: inline; margin-right: 1%"><span style="font-weight: bold;">DATE OF BIRTH :</span></p>
           <select class="formInputDate" name="day" value="" id="day"></select>
           <select class="formInputDate" name="month" id="month">
                              <option value="1">Jan</option>
                              <option value="2">Feb</option>
                              <option value="3">Mar</option>
                              <option value="4">Apr</option>
                              <option value="5">May</option>
                              <option value="6">Jun</option>
                              <option value="7">Jul</option>
                              <option value="8">Aug</option>
                              <option value="9">Sep</option>
                              <option value="10">oct</option>
                              <option value="11">Nov</option>
                              <option value="12">Dec</option>
            </select>
            <select class="formInputDate" name="year" value="" id="year">
            </select>
          </div>

1 个答案:

答案 0 :(得分:0)

更改此行:

 $stmt->bindParam(':DOB', $_POST['year'], $_POST['month'], $_POST['day']);

到:

 $stmt->bindParam(':DOB', $_POST['year'].'-'. $_POST['month'].'-'. $_POST['day']);

原因是因为DATE格式需要正确的格式日期,例如YYYY-MM-DD。