简体   繁体   中英

How can I pass date to MySQL with PHP

I have a createAdmin.php form with this section:

<p>Date of Birth:&nbsp; <input maxlength="4" size="4" type = "text" name = "year" value = "" placeholder="YYYY" > -
                 &nbsp; <input maxlength="2" size="2" type = "text" name = "month" value = "" placeholder="MM" > -
                 &nbsp; <input maxlength="2" size="2" type = "text" name = "day" value = "" placeholder="DD" >

Caching those values:

$year = (int) $_POST['year'];
$month = (int) $_POST['month'];
$day = (int) $_POST['day'];

And finally passing to mySQL:

$formBirth = "{$year}{$month}{$day}";
$createAdmin = mysqli_query($db, "INSERT INTO admins (username, hashed_pwd, power, email, name, birth) VALUES (
'{$username}', '{$password}', '{$power}', '{$email}', '{$realName}', STR_TO_DATE('$formBirth', '%Y%m%d'))");

This method apparently doesn't work. Any ideas?

尝试将所有这些都放入变量中并使用

$date = $year.'-'.$month.'-'.$day;

Try to add / on this one:

$formBirth = $year."/".$month."/".$day;

Or:

$formBirth = "{$year}/{$month}/{$day}";

Or using -

$formBirth = $year."-".$month."-".$day;

Or:

$formBirth = "{$year}-{$month}-{$day}";

See Demo

$formBirth = "{$year}-{$month}-{$day}";
Date of birth<input type="date"  name="date" />(html 5)
<?php
$formBirth = $_POST['date'];
$createAdmin = mysqli_query($db, "INSERT INTO admins (username, hashed_pwd, power, email, name, birth) VALUES (
'{$username}', '{$password}', '{$power}', '{$email}', '{$realName}',{$formBirth})");

?>

OR use this
<?php 
$year =$_POST['year'];
$month =$_POST['month'];
$day =$_POST['day'];
$formBirth=$year."/".$month."/".$day;

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM