...

View Full Version : insert date from form to db



esthera
02-14-2006, 07:49 PM
I have a form taht takes a date with 3 fields

mymonth,myday,myyear

How would I combine these to insert them to a mysql datetimefield -- waht's the correct way to combine them?

Element
02-14-2006, 07:58 PM
well there is plenty of correct ways to do it, but to add it to a datetimefieeld or w/e i think it has to bee numeral. But to combine them it could simply be done like:

have the thre date fields with the name="date[]" and then do something like:





// 1
$date = '';
foreach($_POST['date'] as $value) {
$date .= $value . ' ';
}
$date = trim($date);

// 2
$date = $_POST['date'][0] . ' ' . $_POST['date'][1] . ' ' . $_POST['date'][2];

Kid Charming
02-14-2006, 08:42 PM
MySQL accepts a number of variations of the basic 'YYYYMMDD' format, as far as using delimiters and such. You can use any punctuation as a delimiter, but not spaces -- 'YYYY-MM-DD' or 'YYYY&MM&DD' will work, but 'YYYY MM DD' will not. Delimiters aren't necessary, though, and the db will store them the same way regardless, so there's not much point to using them. You might as well just concatenate your date into one 'YYYYMMDD' string.

esthera
02-15-2006, 05:58 AM
if i do this

$date = '';
foreach($_POST['date'] as $value) {
$date .= $value . ' ';
}
$date = trim($date);

// 2
$date = $_POST['date'][0] . ' ' . $_POST['date'][1] . ' ' . $_POST['date'][2];


do i ahve to put the select boxes in any specific order?

StupidRalph
02-15-2006, 06:44 AM
I have a form taht takes a date with 3 fields

mymonth,myday,myyear


If you have them in th order you describe you can just change the last line of code around from this.
$date = $_POST['date'][0] . ' ' . $_POST['date'][1] . ' ' . $_POST['date']; to be like this
$date = $_POST['date'][2] . '-' . $_POST['date'][0] . '-' . $_POST['date'][1];

The key is in naming all of the select boxes the same name..... name = date[]
And like Kid Charming said, you can use any or no delimiter except for space.(i.e. ' ').



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum