terungwa Posted February 17, 2014 Share Posted February 17, 2014 My date selection list below allows selection of 30th, 31st and 29th february (on non-leap year). Although I noticed the mysql database is treating those entries as invalid and records them as 0000-00-00. How may i remove this from the user input interface? <?php $monthName = array(1 => "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); $today = time(); //stores today's date $f_today = date("M-d-Y",$today); //formats today's date $startyear = date("Y")-60; echo "<div style = 'text-align: center'>\n"; echo "<form action='$_SERVER[PHP_SELF]' method='POST'>\n"; /* build selection list for the month */ $todayMO = date("n",$today); //get the month from $today echo "<select name='month'>\n"; for ($n=1;$n<=12;$n++) { echo " <option value=$n"; if ($todayMO == $n) { echo " selected"; } echo " > $monthName[$n]\n\n</option>"; } echo "</select>\n"; /* build selection list for the day */ $todayDay= date("d",$today); //get the day from $today echo "<select name='day'>\n"; for ($n=1;$n<=31;$n++) { echo " <option value=$n"; if ($todayDay == $n ) { echo " selected"; } echo " > $n</option>\n"; } echo "</select>\n"; /* build selection list for the year */ echo "<select name='year'>\n"; for ($n=$startyear;$n<=$startyear+200;$n++) { echo " <option value=$n"; if ($startyear == $n ) { echo " selected"; } echo " > $n</option>\n"; } echo "</select>\n"; echo "<p> <input type=\"submit\" name=\"insert\" value=\"Insert New Entry\" id=\"insert\"> </p></form></div>\n"; ?> Thanks. Quote Link to comment Share on other sites More sharing options...
terungwa Posted February 17, 2014 Author Share Posted February 17, 2014 Currently, I am subjecting the insert query to the PHP functioncheckdate(), which is smart enough to know when its a leap year and prevents mistakessuch as September 31. but again, If i can remove it from the front-end, the better. if (!checkdate($month,$day,$year)) { echo 'You have used an invalid date'; } else{ // create SQL $sql = 'INSERT INTO date ( date_value ) VALUES(?)'; if ($stmt->prepare($sql)) { // bind parameters and execute statement $stmt->bind_param('s', $date_value); // execute and get number of affected rows $stmt->execute(); if ($stmt->affected_rows > 0) { $OK = true; } } // redirect if successful or display error if ($OK) { header('Location: insert_success.php'); exit; } else { $error = $stmt->error; } }} Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted February 17, 2014 Solution Share Posted February 17, 2014 (edited) Easiest way by far, for you and the user, is to use a datepicker (eg SCW or jQuery datepicker) Edited February 17, 2014 by Barand Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.