Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your question is a bit confusing. Inserting data "by validating it" doesn't make sense.</p> <p>If you want to know how to insert data, well that's a basic MySQL question which is probably best answered by consulting the <a href="http://dev.mysql.com/doc/refman/5.1/en/insert.html" rel="nofollow noreferrer">online documentation</a>.</p> <p>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.</p> <p>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 <a href="http://dev.mysql.com/doc/refman/5.0/en/datetime.html" rel="nofollow noreferrer">DATE/DATETIME type</a>), 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.</p> <p>So your code would look something like:</p> <pre><code>&lt;? $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-&gt;birthday == '0000-00-00') { // submitted date was invalid } ?&gt; </code></pre> <p>Of course, there are many drawbacks to the above code. Ideally, you probably want to validate the data <i>before</i> 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.</p> <p>BTW: I think you meant to say "drop-down list" or "select list." A drop-down menu is something else.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload