thorin
May 10th 2007, 1:52 am
In mySql, can the default value be a combination of other field values ?
I have three fields (enter via a PHP page), for the year,month and day called :-
fldYear
fldMonth
fldDay
These are all selected via drop down boxes (to restrict users inputting the wrong date format)and post to a mySql table, but when I display the table on another page, I want to show only the correct date (which will be sortable).
Does anyone know of a good way to combine these three fields into the one new date field ?
e39m5
May 10th 2007, 3:13 pm
Of course, you just need to combine the variables with PHP before you insert them into the database.
First, I would set your column type to DATE. This uses a YYYY-MM-DD format. Then you can use:
$date = $_POST['fldYear'] . '-' . $_POST['fldMonth'] . '-' . $_POST['fldDay'];
Be sure that your drop down values for month and day are set to 01, 02, etc, and not just 1, 2, etc.
e39m5
thorin
May 10th 2007, 3:50 pm
Thank you, I now have the following section of code :-
$date = $_POST['fldYear'] . '-' . $_POST['fldMonth'] . '-' . $_POST['fldDay'];
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1") && $word_ok=="yes") {
$insertSQL = sprintf("INSERT INTO catches (fldYear, fldMonth, fldDay, name, pegno, nofishca, specie, largewght, catchwght) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['fldYear'], "int"),
GetSQLValueString($_POST['fldMonth'], "int"),
GetSQLValueString($_POST['fldDay'], "int"),
GetSQLValueString($_POST['name'], "text"),
GetSQLValueString($_POST['pegno'], "int"),
GetSQLValueString($_POST['nofishca'], "int"),
GetSQLValueString($_POST['specie'], "text"),
GetSQLValueString($_POST['largewght'], "int"),
GetSQLValueString($_POST['catchwght'], "int"));
mysql_select_db($database_roughamlake, $roughamlake);
$Result1 = mysql_query($insertSQL, $roughamlake) or die(mysql_error());
$insertGoTo = "catch.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
My field name in the mySql table is called datefished, and is set to DATE format, as I am new to PHP, I am unsure how to update the datefished field in the table, can you help ?
asfi
May 10th 2007, 5:58 pm
In mySql, can the default value be a combination of other field values ?
I have three fields (enter via a PHP page), for the year,month and day called :-
fldYear
fldMonth
fldDay
These are all selected via drop down boxes (to restrict users inputting the wrong date format)and post to a mySql table, but when I display the table on another page, I want to show only the correct date (which will be sortable).
Does anyone know of a good way to combine these three fields into the one new date field ?
Yes you can do this by setting your data field using DATE format
gibex
May 10th 2007, 6:33 pm
alter table table_name change 'field_name' 'field_name' field_type;
or using phpmyadmin or whatever mysql interface you have.
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.