mysql phpmyadmin, how to save date of birth that was attained through a form with the input type "select"(drop down)
I have a form which asks the user his or her date of birth. Its a drop down box since I use the select input type. The first drop down has Months, second has Days, the third has the Years. So the format should be Month(October), Day(25), Year(1990). In other words the months spell out the actual month and not simply the number representation of the month. However, I would like to save the Day and Year in their respective formats. How can I do this in phpMyAdmin? Should I use the DATE type? what should the value be? I just need to know how to correlate开发者_运维百科 all of this in phpmyadmin, so I can save it properly. Thank you.
Should I use the DATE type?
Yes and here is how you can concatenate them and store in the database:
$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];
$date = $month . '-' . $day . '-' . $year;
Now you can use the $date
variable in your query against the date
type field.
Not phpmyAdmin but mysql you mean I hope.
Well, you have to distinguish the value of the thing and it's representation.
While value can be always the same, representation may vary.
The only way to store date in MySQL is to store it in the date field, which has strict format:
YYYY-MM-DD
And you have to store your date in this format only. To do this, you have to make your month select like this:
<select name="month">
<option value="1">January</option>
<option value="2">February</option>
...
</select>
So, month will be represented with word, while numeric value will be sent to the server.
Now, along with day and year variables, you can assemble a date string:
$date = $_POST['year']."-". $_POST['month']."-".$_POST['day'];
$date = mysql_real_escape_string($date);
Now you have your date ready to store in the database.
精彩评论