g Void
g Void

Reputation: 171

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?

Upvotes: 0

Views: 156

Answers (4)

Edper
Edper

Reputation: 9322

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

Upvotes: 0

shafi
shafi

Reputation: 310

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;

?>

Upvotes: 0

praveen
praveen

Reputation: 286

Try to take all these into a variable and use that

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

Upvotes: 1

웃웃웃웃웃
웃웃웃웃웃

Reputation: 11984

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

Upvotes: 0

Related Questions