sofia403 Posted May 11, 2011 Share Posted May 11, 2011 hello i have a question. i have a drop down form to select DD-MM-YEAR , but im unsure how to actually post it into one column DATE since values are given in three separate dropdown options? here is an example of my html, and i would like a php code to be able to post that to mysql. i have it working for other dropdown menus, but not sure how would it work with a date. im very new to php and mysql and any help would be much appreciated. thank you. <select name="Date" id="Date" class="regularfont"><option value="" selected="selected"></option> <option value="1">01</option> <option value="2">02</option> </select> <select name="Date" id="Date" class="regularfont"><option value="" selected="selected"></option> <option value="1">January</option> <option value="2">February</option> </select> <select name="Date" id="Date" class="regularfont"><option value="" selected="selected"></option> <option value="2014">2014</option> <option value="2013">2013</option> Quote Link to comment https://forums.phpfreaks.com/topic/236116-date-post-to-db/ Share on other sites More sharing options...
fugix Posted May 11, 2011 Share Posted May 11, 2011 firstly change you select names to "Day" "Month" "Year" accordingly, then you could simply combine the 3 to form a legitimate date to insert into your db. like so $date = $_POST['day'] . "-" . $_POST['Month'] . "-" . $_POST['Year']; Quote Link to comment https://forums.phpfreaks.com/topic/236116-date-post-to-db/#findComment-1213885 Share on other sites More sharing options...
sofia403 Posted May 11, 2011 Author Share Posted May 11, 2011 thanks for quick response fugix, however im having troubles still, not sure if i have everything entered correctly though. i have a form which is being posted by php to mysql here is my html: <form name="DATE1" method="post"> <div><select name="day" id="Date1" class="regularfont"><option value="" selected="selected"></option> <option value="1">01</option> <option value="2">02</option> </select> <select name="month" id="Date2" class="regularfont"><option value="" selected="selected"></option> <option value="1">January</option> <option value="2">February</option> </select> <select name="year" id="Date3" class="regularfont"><option value="" selected="selected"></option> <option value="2014">2014</option> <option value="2013">2013</option> on submit it passed on to php <?php $Date1 = $_POST['day'] . "-" . $_POST['month'] . "-" . $_POST['year']; ?> what am i doing wrong ? Quote Link to comment https://forums.phpfreaks.com/topic/236116-date-post-to-db/#findComment-1213903 Share on other sites More sharing options...
fugix Posted May 11, 2011 Share Posted May 11, 2011 thanks for quick response fugix, however im having troubles still, not sure if i have everything entered correctly though. i have a form which is being posted by php to mysql here is my html: <form name="DATE1" method="post"> <div><select name="day" id="Date1" class="regularfont"><option value="" selected="selected"></option> <option value="1">01</option> <option value="2">02</option> </select> <select name="month" id="Date2" class="regularfont"><option value="" selected="selected"></option> <option value="1">January</option> <option value="2">February</option> </select> <select name="year" id="Date3" class="regularfont"><option value="" selected="selected"></option> <option value="2014">2014</option> <option value="2013">2013</option> on submit it passed on to php <?php $Date1 = $_POST['day'] . "-" . $_POST['month'] . "-" . $_POST['year']; ?> what am i doing wrong ? well, what is happening when the form is sent? Quote Link to comment https://forums.phpfreaks.com/topic/236116-date-post-to-db/#findComment-1213910 Share on other sites More sharing options...
sofia403 Posted May 11, 2011 Author Share Posted May 11, 2011 i have some other forms in a dropdown menu with different form names those get passed on and entered into mysql by php, but the date one doesnt get updated, just 00-00-0000 should i post the whole html and php? Quote Link to comment https://forums.phpfreaks.com/topic/236116-date-post-to-db/#findComment-1213920 Share on other sites More sharing options...
fugix Posted May 11, 2011 Share Posted May 11, 2011 i have some other forms in a dropdown menu with different form names those get passed on and entered into mysql by php, but the date one doesnt get updated, just 00-00-0000 should i post the whole html and php? yes if you wouldn't mind, so i can see how you are going about this Quote Link to comment https://forums.phpfreaks.com/topic/236116-date-post-to-db/#findComment-1213930 Share on other sites More sharing options...
sofia403 Posted May 11, 2011 Author Share Posted May 11, 2011 here is my HTML <html> <head> <?php $hostname = "localhost"; $db_user = ""; $db_password = ""; $database = ""; $db_table = ""; $db = mysql_connect($hostname, $db_user, $db_password); mysql_select_db($database,$db); ?> <script type="text/javascript" src="js/jquery-1.5.2.min.js"></script> <script type="text/javascript" src="js/jquery.validate.min.js"></script> <script type="text/javascript" src="js/additional-methods.min.js"></script> </head> <body> <form id="signupForm" method="POST" action="processform.php"> <fieldset> <legend>Signup Form</legend> <label for="Country">*Country:</label> <form name="Country" method="post"> <div><select name="Country"> <option value="">-----</option> <option value="Canada">Canada</option> <option value="UK">UK</option> <option value="France">France</option></select></div></p> <p><label for="Nationality">*Nationality:</label> <form name="Nationality" method="post"> <div><select name="Nationality"> <option value="">-----</option> <option value="Afghanistan">Afghanistan</option> <option value="Albania">Albania</option> <option value="Algeria">Algeria</option></select></div></p> <p><label for="Province">*Province:</label> <form name="Province" method="post"> <div><select name="Province"> <option value="">-----</option> <option value="British Columbia">British Columbia</option> <option value="Alberta">Alberta</option> <option value="Saskatchewan">Saskatchewan</option></select></div></p> <p><label for="DATE1">*Date:</label> <form name="DATE1" method="post"> <div><select name="day" id="Date1" class="regularfont"><option value="" selected="selected"></option> <option value="1">01</option> <option value="2">02</option> <option value="3">03</option></select> <select name="month" id="Date2" class="regularfont"><option value="" selected="selected"></option> <option value="1">January</option> <option value="2">February</option> <option value="3">March</option></select> <select name="year" id="Date3" class="regularfont"><option value="" selected="selected"></option> <option value="2014">2014</option> <option value="2013">2013</option> <option value="2012">2012</option></select></div></p></fieldset> <p> <div align="center"><input type="submit" name="submit" value="Submit your entry"></div> </form> </div></p><?php } ?> </body> </html> and my PHP processform.php <?php $hostname = "localhost"; $db_user = ""; $db_password = ""; $database = ""; $db_table = ""; $db = mysql_connect($hostname, $db_user, $db_password); mysql_select_db($database,$db); ?> <?php if (isset($_REQUEST['submit'])) { $sql = "INSERT INTO $db_table(Country,Nationality,Province,DATE1) values ('".mysql_real_escape_string(stripslashes($_REQUEST['Country']))."','".mysql_real_escape_string(stripslashes($_REQUEST['Nationality']))."','".mysql_real_escape_string(stripslashes($_REQUEST['Province']))."','".mysql_real_escape_string(stripslashes($_REQUEST['DATE1']))."')"; if($result = mysql_query($sql ,$db)) { echo '<h1>Success!</h1>'; } else { echo "ERROR: ".mysql_error(); } } else ?> <?php $DATE1= $_POST['day'] . "-" . $_POST['month'] . "-" . $_POST['year']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/236116-date-post-to-db/#findComment-1213954 Share on other sites More sharing options...
fugix Posted May 11, 2011 Share Posted May 11, 2011 what is the format for the column "DATE1", meaning do you have it set as date, varchar, etc... Quote Link to comment https://forums.phpfreaks.com/topic/236116-date-post-to-db/#findComment-1213958 Share on other sites More sharing options...
sofia403 Posted May 11, 2011 Author Share Posted May 11, 2011 its set to date on the server Quote Link to comment https://forums.phpfreaks.com/topic/236116-date-post-to-db/#findComment-1213962 Share on other sites More sharing options...
fugix Posted May 11, 2011 Share Posted May 11, 2011 if the column_is set to DATE, then you will need to insert it into the db using the format "yyyy-mm-dd" Quote Link to comment https://forums.phpfreaks.com/topic/236116-date-post-to-db/#findComment-1213975 Share on other sites More sharing options...
sofia403 Posted May 11, 2011 Author Share Posted May 11, 2011 ok good to know, i actually changed my mind and set it to varchar now but dont get any update in it. empty Quote Link to comment https://forums.phpfreaks.com/topic/236116-date-post-to-db/#findComment-1213977 Share on other sites More sharing options...
fugix Posted May 11, 2011 Share Posted May 11, 2011 your $date1 variable is what needs to be inserted into your db..not sure why that code is at the bottom, it needs to be before your insert and the $DATE1 var is to be inserted Quote Link to comment https://forums.phpfreaks.com/topic/236116-date-post-to-db/#findComment-1213986 Share on other sites More sharing options...
xyph Posted May 11, 2011 Share Posted May 11, 2011 Echo your query before you submit it. Plug it into a raw query line (like from the console or PHPMyAdmin) and see what errors are reported. Chances are there is a problem with your query. Quote Link to comment https://forums.phpfreaks.com/topic/236116-date-post-to-db/#findComment-1213992 Share on other sites More sharing options...
fugix Posted May 11, 2011 Share Posted May 11, 2011 Echo your query before you submit it. Plug it into a raw query line (like from the console or PHPMyAdmin) and see what errors are reported. Chances are there is a problem with your query. this is already implemented in her code Quote Link to comment https://forums.phpfreaks.com/topic/236116-date-post-to-db/#findComment-1213993 Share on other sites More sharing options...
sofia403 Posted May 11, 2011 Author Share Posted May 11, 2011 Hi do you mean just move my php code for DATE1 up before the insert one? im sorry i probably come on as a total noob, which Iam Quote Link to comment https://forums.phpfreaks.com/topic/236116-date-post-to-db/#findComment-1214000 Share on other sites More sharing options...
fugix Posted May 11, 2011 Share Posted May 11, 2011 yeah, use this <?php if (isset($_REQUEST['submit'])) { $date1= $_POST['day'] . "-" . $_POST['month'] . "-" . $_POST['year']; $sql = "INSERT INTO $db_table(Country,Nationality,Province,DATE1) values ('".mysql_real_escape_string(stripslashes($_REQUEST['Country']))."','".mysql_real_escape_string(stripslashes($_REQUEST['Nationality']))."','".mysql_real_escape_string(stripslashes($_REQUEST['Province']))."','$date1')"; if($result = mysql_query($sql ,$db)) { echo '<h1>Success!</h1>'; } else { echo "ERROR: ".mysql_error(); } } else ?> Quote Link to comment https://forums.phpfreaks.com/topic/236116-date-post-to-db/#findComment-1214003 Share on other sites More sharing options...
PaulRyan Posted May 11, 2011 Share Posted May 11, 2011 While reading your PHP code I noticed alot of silly errors and mistakes, I've corrected everything I could see for you. HTML Page <html> <head> <script type="text/javascript" src="js/jquery-1.5.2.min.js"></script> <script type="text/javascript" src="js/jquery.validate.min.js"></script> <script type="text/javascript" src="js/additional-methods.min.js"></script> </head> <body> <form id="signupForm" method="POST" action="processform.php"> <div> <fieldset> <legend>Signup Form</legend> <label for="Country">*Country:</label> <select name="Country"> <option value="">-----</option> <option value="Canada">Canada</option> <option value="UK">UK</option> <option value="France">France</option></select></div></p> <p><label for="Nationality">*Nationality:</label> <div><select name="Nationality"> <option value="">-----</option> <option value="Afghanistan">Afghanistan</option> <option value="Albania">Albania</option> <option value="Algeria">Algeria</option></select></div></p> <p><label for="Province">*Province:</label> <div><select name="Province"> <option value="">-----</option> <option value="British Columbia">British Columbia</option> <option value="Alberta">Alberta</option> <option value="Saskatchewan">Saskatchewan</option></select></div></p> <p><label for="DATE1">*Date:</label> <div><select name="day" id="Date1" class="regularfont"><option value="" selected="selected"></option> <option value="1">01</option> <option value="2">02</option> <option value="3">03</option></select> <select name="month" id="Date2" class="regularfont"><option value="" selected="selected"></option> <option value="1">January</option> <option value="2">February</option> <option value="3">March</option></select> <select name="year" id="Date3" class="regularfont"><option value="" selected="selected"></option> <option value="2014">2014</option> <option value="2013">2013</option> <option value="2012">2012</option></select> </fieldset> </div> <p> <div align="center"><input type="submit" name="submit" value="Submit your entry"></div> </p> </form> <p> </body> </html> PHP Processing Page <?PHP $hostname = "localhost"; $db_user = ""; $db_password = ""; $database = ""; $db_table = ""; $db = mysql_connect($hostname, $db_user, $db_password); mysql_select_db($database,$db); if(isSet($_POST['submit'])) { $country = mysql_real_escape_string($_POST['Country']); $nationality = mysql_real_escape_string($_POST['Nationality']); $province = mysql_real_escape_string($_POST['Province']); $date = $_POST['day'].'-'.$_POST['month'].'-'.$_POST['year']; $myQuery = "INSERT INTO {$db_table} (`Country`,`Nationality`,`Province`,`DATE1`) VALUES ('{$country}','{$nationality}','{$province}','{$date}')"); if(mysql_query($myQuery)) { echo 'Record inserted.'; } else { echo 'An error has occurred: '.mysql_error(); } } ?> Try using the above code and tell me how it goes. Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/236116-date-post-to-db/#findComment-1214010 Share on other sites More sharing options...
sofia403 Posted May 11, 2011 Author Share Posted May 11, 2011 fugix it works! thanks for all your help! and thanks PaulRayn for correcting my mistakes, much appreciated! im trying to work on silly part Quote Link to comment https://forums.phpfreaks.com/topic/236116-date-post-to-db/#findComment-1214017 Share on other sites More sharing options...
fugix Posted May 11, 2011 Share Posted May 11, 2011 fugix it works! thanks for all your help! and thanks PaulRayn for correcting my mistakes, much appreciated! im trying to work on silly part glad I could help, if you have any more problems we'll be here! Quote Link to comment https://forums.phpfreaks.com/topic/236116-date-post-to-db/#findComment-1214019 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.