Forums
This topic is locked
Insert date to MySQL DB using split function
Posted 22 Nov 2006 00:55:12
1
has voted
22 Nov 2006 00:55:12 Robert Robinette posted:
Using MX | PHP | MySQL
I'm new to PHP and trying to insert insert values into a MySql DB where two of the fields are dates. (startDate and endDate) Those two date values are passed to to the form as URL variables and populate the fields when the page is displayed. I need to use the split function to take the date in mm/dd/yyyy format and enter it into the DB in yyyy-mm-dd format. Could someone take a look at my code and see if I'm on the right track? Thanks!
<pre id=code><font face=courier size=2 id=code>
$startDate = '01/01/2007';
if (isset($HTTP_GET_VARS['startDate'])) {
$startDate = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['startDate'] :
stripslashes($HTTP_GET_VARS['startDate']);
}
list($sMonth, $sDay, $sYear) = split('[/.-]', $startDate);
if (is_numeric($sMonth) && is_numeric($sDay) && is_numeric($sYear)) {
$sDate = "$sYear-$sMonth-$sDay";
}
else {
echo 'Invalid start date';
exit;
}
$endDate = '01/02/2007';
if (isset($HTTP_GET_VARS['endDate'])) {
$endDate_rsAV = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['endDate'] :
stripslashes($HTTP_GET_VARS['endDate']);
}
list($eMonth, $eDay, $eYear) = split('[/.-]', $endDate);
// check that date elements are all numbers
if (is_numeric($eMonth) && is_numeric($eDay) && is_numeric($eYear)) {
$eDate = "$eYear-$eMonth-$eDay";
}
else {
echo 'Invalid end date';
exit;
}
if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "bookroom") {
$insertSQL = sprintf("INSERT INTO bookings (roomID, clientID, startDate, endDate, adults, children) VALUES (%s, %s, %s, %s, %s, %s)",
GetSQLValueString($HTTP_POST_VARS['roomID'], "text",
GetSQLValueString($HTTP_POST_VARS['clientID'], "text",
GetSQLValueString($HTTP_POST_VARS['$sDate'], "date",
GetSQLValueString($HTTP_POST_VARS['$eDate'], "date",
GetSQLValueString($HTTP_POST_VARS['adults'], "int",
GetSQLValueString($HTTP_POST_VARS['children'], "int");
mysql_select_db($database_connGR, $connGR);
$Result1 = mysql_query($insertSQL, $connGR) or die(mysql_error());
</font id=code></pre id=code>