【问题标题】:DOB mysql insertDOB mysql 插入
【发布时间】:2017-10-17 00:32:13
【问题描述】:

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

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

提前感谢,抱歉英语不好=(

下面是我的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 中的下拉代码

<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.错误是什么? 2.你对bindParam(':DOB', $_POST['year'], $_POST['month'], $_POST['day'])有什么期望?
  • 这看起来不对...:month, :year, DOB) 缺少冒号?
  • 感谢回复,我对php很陌生,打印出错误的命令是因为现在它只是没有将数据插入数据库但没有显示任何内容,

标签: php html mysql pdo


【解决方案1】:

改变这一行:

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

到:

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

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

【讨论】:

  • 非常感谢您的回复,我试过了但还是不行,我在想是不是和$year = $_POST['year']; $month = $_POST['month']; $date = $_POST['date']; $DOB = date("Y-m-d", mktime(0,0,0,$year, $month, $day));有关,我不确定是否有必要声明变量与否,我对 php 很陌生 =( @Simos Fasouliotis
  • 我确实修复了它,首先声明 $DOB = date("Y-m-d", strtotime( $_POST['year'].'-'. $_POST['month'].'-'. $_POST['day'])); 然后 $stmt-&gt;bindParam(':DOB', $DOB); 非常感谢您的帮助^^"
猜你喜欢
  • 2018-10-19
  • 2011-02-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多