Rocky
Rocky

Reputation: 4601

How to validate drop down menus in php?

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?


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

Upvotes: 0

Views: 3238

Answers (4)

Calvin
Calvin

Reputation: 4619

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.

Upvotes: 0

Judson
Judson

Reputation: 2325

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.

Upvotes: 0

Andrei Serdeliuc ॐ
Andrei Serdeliuc ॐ

Reputation: 5878

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 . "')";

Upvotes: 1

sanders
sanders

Reputation: 10898

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

Upvotes: -1

Related Questions