gpt4 book ai didi

php - DOB mysql插入

转载 作者:行者123 更新时间:2023-11-30 21:59:10 28 4
gpt4 key购买 nike

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

附言。但是,它确实插入到我的 没问题。

提前致谢,抱歉英语水平不佳=(

下面是我的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>

最佳答案

改变这一行:

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

到:

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

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

关于php - DOB mysql插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44026652/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com