开发者

php insert date of birth into mysql

I am using html <select>, <option> form to collect data from users. However, I got stuck when trying to save the day of birth to the database. Here is what开发者_StackOverflow I am trying to do:

$dob = $_POST['year'] . '-' . $_POST['month'] . '-' .$_POST['day'];

$insertInto = " INSERT INTO `users`.`information` (id, useremail, password, firstname, lastname, gender, dob, telephone)
VALUES ( NULL, '$_POST[email]','$_POST[password]','$_POST[firstname]','$_POST[lastname]','$_POST[formGender]','$dob', '$_POST[telephone]')";

It works for everything but the $dob, could someone guide me on finding the error?

Thank you very much.


If you don't mind using unix's epoch, you can simply take your users input and do a simple strtotime(). It'll give you more flexibility on pulling it out and how you can display it using the date() function.

$dob = "1/1/1985";
$string = strtotime($dob);

then to spit it out:

$data = pull epoch result from database
$birthday = date("m/d/Y", $data);


First check the value of $dob before doing the query call. Then, if it looks alright (1984-10-10 for example), make your dob is a date/datetime.

By now, what error are you getting? Is it being set to all 0's or what exactly?

Note: you will be exposed to SQL injection. I would always recommend using PDO with prepared statements for nearly any SQL query. http://mx.php.net/manual/en/pdo.prepare.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜