tags:

views:

996

answers:

6

Its like we have to insert the Date of Birth(which is a drop down box containing DD/MM/YYYY) in MySql by validating it..How can it be done? Thanks for the help.

A: 

What kind of validation? mysql will print an error when the date is not valid

sanders
A: 

Its a form validation for Date of Birth which is input by user from the options in dropdown menu.My question is how to insert that input DOB in MySql using PHP

Rocky
+1  A: 

You could first of all do (for each field):

if((int) $field == 0)
{
    return false;
}

And after that you could do:

$birthday = date('Y-m-d', strtotime($day . '-' . $month . '-' . $year));
mysql_query("INSERT INTO your_table(birthday) VALUES('" . $birthday . "')";
Andrei Serdeliuc
A: 

I assume by drop down you mean a series of selects like this, right?

<select name="month">
<option value ="01">January</option>
<option value = "02">February<option>
...
</select>
<select name="day">
<option value = "01">01</option>
...
</select>
<select name="year">
<option value = "1930">1930</option>
[etc.]

If that's the case, then your data is self-validating. On the server side, you could just do:

<?php
$mysql_date = "$_POST[year]" . "-" . "$_POST[month]" . "-" . "$_POST[day]";
mysql_query("INSERT INTO your_table (`date`) VALUES ('$mysql_date')");
?>

Hope I didn't misunderstand the question.

Judson
That's misleading, that's not self-validating in a secure way. Someone could easily submit something not defined within the HTML and attempt to exploit your query. Never rely on a html form/select to validate.
Adam Gibbins
A: 

Your question is a bit confusing. Inserting data "by validating it" doesn't make sense.

If you want to know how to insert data, well that's a basic MySQL question which is probably best answered by consulting the online documentation.

If you want to know how to validate the data, you're going to have to specify the kind of validation you want--as sanders has answered. It's sort of like asking "how do I make a GUI?" There are infinite ways you can do it. The question is too vague.

If you just want to check to see if the value submitted is a valid date, a quick and dirty (lazy) way of doing it is just to insert it into the table (assuming you have a column of the DATE/DATETIME type), then make a follow-up query to retrieve the value stored. If the stored value is that data type's "zero" value (i.e. '0000-00-00' or '0000-00-00 00:00:00'), then the submitted value was not a valid date.

So your code would look something like:

<?
$month = mysql_real_escape_string($_POST['month']);
$day = mysql_real_escape_string($_POST['day']);
$year = mysql_real_escape_string($_POST['year']);
$query = "INSERT INTO my_table (`birthday`) VALUES ('$year-$month-$day')";
mysql_query($query);
$query = "SELECT `birthday` FROM my_table WHERE `id`=LAST_INSERT_ID()";
$qid = mysql_query($query);
$res = mysql_fetch_object($query);
if ($res->birthday == '0000-00-00') {
    // submitted date was invalid
}
?>

Of course, there are many drawbacks to the above code. Ideally, you probably want to validate the data before you insert it, and if you are updating a record rather than inserting a new one, then you will need to fetch the original value before it is overwritten with an invalid one.

BTW: I think you meant to say "drop-down list" or "select list." A drop-down menu is something else.

Calvin
A: 

I think "validation" in this question was meant as the most basic one: "required field". If user doesn't select an option and leaves the first one (0, for example "Please choose") the form should give him a a message above the field itself, like "you forgot to choose..." How would you do that? I guess this was the main point :-) Cheers,

Panamon

PS: I am interested in this too: if you're able to do it and feel like to help me, please drop me some hints!

panamon 23082 @@@ yahoo . it (just one @, and with0ut sp4aces)