Jump to content

PHP Date Selection Problem


amin1982

Recommended Posts

hey Guys,

 

The code below allows the admin to create a new event which goes straight to their database. We have recently moved to a new server and now when we enter the date rather than taking the date we enter it picks up todays date instead.

 

The date is required to be filled in the format of ddmmyy.

 

Again this is an old code (I believe PHP3 with there server being at least PHP4)

 

CODE:

 

<?php
include("functions.php");

$editFormAction = $HTTP_SERVER_VARS['PHP_SELF'];
if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
  $editFormAction .= "?" . $HTTP_SERVER_VARS['QUERY_STRING'];
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $myFolder = (!empty($_POST['image_folder'])?$_POST['image_folder']$_POST['event_date']."_".$_POST['venue_id']));
  $insertSQL = sprintf("INSERT INTO ec_events (event_name, venue_id, event_date, image_folder) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($_POST['event_name'], "text"),
                       GetSQLValueString($_POST['venue_id'], "int"),
                       GetSQLValueString((isset($_POST['nodate'])?"":$_POST['event_date']), "date"),
                       GetSQLValueString($myFolder, "text"));

  mysql_select_db($database_elite, $elite);
  $Result1 = mysql_query($insertSQL, $elite) or die(mysql_error());

  $myFolder = "../photos/" . $myFolder;
  if(!file_exists($myFolder)){
      mkdir($myFolder, 0777);
  }
  
  $insertGoTo = "events.php";
  if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $HTTP_SERVER_VARS['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_elite, $elite);
$query_events = "SELECT * FROM ec_events";
$events = mysql_query($query_events, $elite) or die(mysql_error());
$row_events = mysql_fetch_assoc($events);
$totalRows_events = mysql_num_rows($events);

mysql_select_db($database_elite, $elite);
$query_venues = "SELECT * FROM ec_venues ORDER BY ec_venues.venue_name";
$venues = mysql_query($query_venues, $elite) or die(mysql_error());
$row_venues = mysql_fetch_assoc($venues);
$totalRows_venues = mysql_num_rows($venues);

//read event folders
if ($dir = @opendir("../photos")) 
{
while (($file = readdir($dir)) !== false) 
{
if($file != ".." && $file != ".")
{
$filelist[] = $file;
}
} 
closedir($dir);
}
?>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../elite.css" rel="stylesheet" type="text/css">
</head>

<body>
<form name="form1" method="POST" action="<?php echo $editFormAction; ?>">
  <table border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#666699">
    <tr>
      <td bgcolor="#FFFFFF"> 
        <table border="0" cellpadding="0" cellspacing="10" bgcolor="#FFFFFF">
    <tr>
      <td colspan="2" class="heading">Add an event</td>
      </tr>
    <tr> 
      <td align="right" id="l0">Event Date (ddmmyy):</td>
      <td><input name="event_date" type="text" id="i0" size="8" maxlength="6" onKeyPress="document.form1.nodate.checked=false">
 
<input type="checkbox" name="nodate" value="1" onClick="if(document.form1.nodate.checked==true){document.f  orm1.event_date.value=''}">
 No date</td>
    </tr>
    <tr> 
      <td align="right" id="l1">Event Venue (50 max):</td>
      <td><select name="venue_id">
      <?php
do {  
?>
      <option value="<?php echo $row_venues['venue_id']?>"<?php if (!(strcmp($row_venues['venue_id'], $_GET['v']))) {echo "SELECTED";} ?>><?php echo $row_venues['venue_name']?></option>
      <?php
} while ($row_venues = mysql_fetch_assoc($venues));
  $rows = mysql_num_rows($venues);
  if($rows > 0) {
      mysql_data_seek($venues, 0);
      $row_venues = mysql_fetch_assoc($venues);
  }
?>
      </select>
        </td>
    </tr>
    <tr> 
      <td align="right" id="l2">Event Title (100 max):</td>
      <td><input name="textfield" type="text" size="40"></td>
    </tr>
    <tr><td align="right" id="l2">Image Folder</td>
        <td><select name="image_folder" id="image_folder">
        <option value="">Auto</option>
<?php 
asort($filelist);
while (list ($key, $val) = each ($filelist)) 
{
echo "<option value=\"$val\">$val</option>";
}
?>
        </select>        
          </td>
    </tr>
    <tr align="center"> 
      <td colspan="2"> <input type="submit" name="Submit" value="Submit">
    
<input type="button" name="Submit2" value="Cancel" onClick="location.href='events.php'"> 
      </td>
      </tr>
  </table></td></tr></table>
  <input type="hidden" name="MM_insert" value="form1">
</form>

</body>
</html>
<?php
mysql_free_result($events);

mysql_free_result($venues);
?> 

 

Any suggestions why this may be occuring and what i need to do to rectify it?

 

I'm not much of a programmer so any help will be appreciated

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/126754-php-date-selection-problem/
Share on other sites

HTTP_xxxxx_VARS are depreciated, turned off by default in php5, and removed in php6. Add the following two lines immediately after your first <?php tag to see any errors related to this -

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

$HTTP_SERVER_VARS should be changed to $_SERVER

 

hey thanks for your help.

 

i've replaced all the $HTTP_SERVER_VARS with $_SERVER and added the code to see the errors.

 

I received this:

 

Notice: Undefined index: event_name in /home/dan8459/public_html/administer/new_event.php on line 17

 

Warning: Cannot modify header information - headers already sent by (output started at /home/public_html/administer/new_event.php:17) in /home/public_html/administer/new_event.php on line 35

 

However this does not affect the creation of a new event. The event is created ok and displays on the site however the issue with te date is still there.

 

I entered Saturdays date as 270809 but it chose to use todays date

 

I going to guess your form is not supplying the data that it is supposed to. Add the following lines to your form processing code to see what it is receiving -

 

echo "<pre>";
echo "POST:";
print_r($_POST);
echo "</pre>";

 

I'm also going to guess that the date format might be invalid and the GetSQLValueString() function could be replacing it with the current date. Post the code for the GetSQLValueString() function.

 

ok i added that code and i received this:

 

POST:Array

(

    [event_date] => 270908

    [venue_id] => 30

    [textfield] =>

    [image_folder] => valbonne270908

    [submit] => Submit

    [MM_insert] => form1

)

 

 

which i guess means that the code is date is going in OK - yet still todays date is selected.

 

the functions.php code is:

 

<?
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . date('Ymd',strtotime(substr($theValue,4,2).substr($theValue,2,2).substr($theValue,0,2))) . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
?>

The format that is being supplied to the strtotime() function in the 'date' part of the GetSQLValueString() function does not match any of the supported formats and strtotime() is probably substituting portions of the current date for what it cannot figure out. I am not aware of any changes made to the strtotime function, but the following line of code makes the format fit one of the supported types and returns the correct results -

 

$theValue = ($theValue != "") ? "'" . date('Ymd',strtotime(substr($theValue,4,2) . '-' . substr($theValue,2,2) . '-' . substr($theValue,0,2))) . "'" : "NULL";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.