We help IT Professionals succeed at work.

Would like to convert information captured from a from to MySQL database.

LB1234
LB1234 asked
on
200 Views
Last Modified: 2014-08-18
I have a table with a field called "birthday" set to the date, data type.  

I have a form which collects the following:

$month = $_POST["month"];
$day = $_POST["day"]; 
$year = $_POST["year"];	

Open in new window


the format for each is as follows:

Month:
    <option value="01">January</option>, etc.

Day (single digit values for $x are 1, 2, 3, (not 01, 02, 03) etc:
<select name="day" id="day"> <?php 
 for ($x=1; $x<=31; $x++) {
 echo "<option value='$x'>$x</option>";
 }
 ?>
</select>

Open in new window



<input name ="year" value = "1980" type="text">

Open in new window

   
How to write an effective query?

i tried strtotime ("$month $day $year")

but that didn't work.

Thanks.
Comment
Watch Question

CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014

Commented:
What are you trying to do?
Take the selected values in the dropdown and save them in the MySQL table?

Author

Commented:
These values are from different means, but i've already got the values in the variables listed below.  I need to know how to get the variable's values into a format MySQL will accept, and the syntax of the function i need.

$month = $_POST["month"];
$day = $_POST["day"]; 
$year = $_POST["year"];	

Open in new window

Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
I didn't test it but this should work:

$d =$year . '-' . $month . '-' . str_pad($day, 2, '0', STR_PAD_LEFT);
 

Open in new window


Then use $d in your query.
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Please take a moment to read this article.  It will save you a ton of work once you know how PHP handles this DATE/TIME stuff.  Pay particular attention to the ISO-8601 format for datetime values.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Next, take a moment to experiment with form input using free-form dates.  You'll quickly conclude that there is no need for elaborate input controls -- just use the PHP tools and your clients will love you for it!.
http://iconoun.com/demo/strtotime.php
Most Valuable Expert 2011
Author of the Year 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Awesome Ray!  Thanks. Question though...

My table is listed below; please note the number format.  Your code outputs the following : 2000-10-01T00:00:00+02:00

Will that be an issue?

table desc

Author

Commented:
Thanks Ray!
Most Valuable Expert 2011
Author of the Year 2014

Commented:
That should not be an issue if the birthday column is defined as DATE or DATETIME.  MySQL leans toward silent truncation of data elements that exceed the width of the column.

Thanks for the points and best of luck with your project, ~Ray
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.