<html>
<head>
  <title>Customer Subscription Info</title>
</head>
<body>
<h1>Subscription Info Results</h1>
<?


  echo "firstname $firstname <br>";
  echo "lastname $lastname <br>";
  echo "address1 $address1 <br>";
  echo "city $city <br>";
  echo "zip $zip <br>";
  echo "email $email <br>";
  echo "state $state <br>";
  echo "country $country <br>";
  echo "fax $fax <br>";
 
  echo "<p>";
  if (!$firstname || !$lastname || !$address1 || !$city || !$zip  || !$email)
  {
     echo "You have not entered all the required details.<br>"
          ."Please go back and try again.";
     exit;
  }
 
  $firstname = addslashes($firstname);
  $lastname = addslashes($lastname);
  $title = addslashes($title);
  $company = addslashes($company);
  $address1 = addslashes($address1);
  $address2 = addslashes($address2);
  $city = addslashes($city);
  $state = addslashes($state);
  $zip = addslashes($zip);
  $country = addslashes($county);
  $telephone = addslashes($telephone);
  $fax = addslashes($fax);
  $email = addslashes($email);
  $table = 'customers';
  $heading1 = 'First Name';
  $heading2 = 'Last Name';
  $heading3 = 'Title';
  $heading4 = 'Company';
  $heading5 = 'Address1';
  $heading6 = 'Address2';
  $heading7 = 'City';
  $heading8 = 'State';
  $heading9 = 'Zip';
  $heading10 = 'Country';
  $heading11 = 'Telephone';
  $heading12 = 'Fax';
  $heading13 = 'Email';
 
///connect to the mysql server///

  @ $db = mysql_pconnect("localhost", "root");

  if (!$db)
  {
     echo "Error: Could not connect to server/database.  Please try again later.";
     exit;
  }



//debug tool //  echo "we get to the selecting the database";

/// choose the database to be queried ///

  mysql_select_db("newsub2");


///identify location outside web tree to save file to be used to create excel file
$downloadfile = "c:\data\customerfile\customerlist.txt";


///make it available for writing/appending
$fp=fopen("$downloadfile","a");







///retrieving the column headings for the top of the excel worksheet///

$query = "select * from customers";
     $result = mysql_query($query);

$numresults = mysql_num_rows($result);
//echo "numresults " . $numresults;


if($numresults == 0)
   {
    /* get column metadata */
    $i = 0;
    $numfields = mysql_num_fields($result);

    while ($i < mysql_num_fields($result))
          {
            //echo "Information for column $i:<br />\n";
            $meta = mysql_fetch_field($result, $i);
            if (!$meta)
              {
                echo "No information available<br />\n";
                }
            else
                {
                  echo " $meta->name,";
                  $contents1 = $meta->name . ",";
               fwrite($fp,$contents1);
               $i++;
               }
              }
       echo "<br>";       
       fwrite($fp,"\r\n");
       
       $query = "insert into customers values ('".$customerid."', '".$firstname."', '".$lastname."', '".$title."','".$company."', '".$address1."', '".$address2."', '".$city."','".$state."', '".$zip."', '".$country."', '".$telephone."','".$fax."', '".$email."')";
       $result = mysql_query($query);
       
       
       $query = "select * from customers";
       $result = mysql_query($query);   
       
       while($row = mysql_fetch_row($result))
          {
           for ($j=0; $j < $numfields; $j++)
                {
                 if($row[$j] == null)
                  {
                   echo "     ,";
                       $contents2 = $row[$j] . "   ,";
                     fwrite($fp,$contents2);
                  }
                  else
                     {
                      echo "$row[$j],";
                      $contents3 = $row[$j].",";
                     fwrite($fp,$contents3);
                     }
                
                 }  // end of for loop
                   echo "<br>";
            } // end of while loop
            fwrite($fp,"\r\n");

    }
 else
    {
     $query = "insert into customers values ('".$customerid."', '".$firstname."', '".$lastname."', '".$title."','".$company."', '".$address1."', '".$address2."', '".$city."','".$state."', '".$zip."', '".$country."', '".$telephone."','".$fax."', '".$email."')";


     $result = mysql_query($query);

         // freeing memory for additional queries//
         //mysql_free_result($result);
        

                        /* testing code  
                         if ($result)
                              {
                               echo "howdy ";
                               echo  mysql_affected_rows()." customer info inserted into database.";
                               echo "<P>";
                               }
                           else
                               {
                               echo " what\'s up doc?";
                               }
                         */


          ///finished loading new values into customer database///





     $query = "select * from customers";
     $result = mysql_query($query);


      $numrows = mysql_num_rows($result);
       //echo "number of rows "  . $numrows;  //debug tool//
     $rowcounter = 0;

          //$numfields =14;


     //echo "numfields" . $numfields; // debug tool//

     if(!$fp)
       {
        echo"File ain't open";
        exit;
       }

      $numfields = mysql_num_fields($result);
     while($row = mysql_fetch_row($result))
          {
           $rowcounter++;
           //debug tool//echo "rowcounter" .$rowcounter. "numrows" . $numrows;
           
           for ($j=0; $j < $numfields; $j++)
                {
                 if($row[$j] == null)
                  {
                   echo "     ,";
                   if($rowcounter == $numrows)
                     {                   
                      $contents2 = $row[$j] . "   ,";
                      fwrite($fp,$contents2);
                      }
                    }
                  else
                     {
                      echo "$row[$j],";
                     if($rowcounter  == $numrows)
                        {                       
                            $contents3 = $row[$j].",";
                         //debug tool// echo "writing to file  ";
                           fwrite($fp,$contents3);
                        }
                      }
                
                 }  // end of for loop
                   echo "<br>";
            } // end of while loop
            fwrite($fp,"\r\n");
     fclose($fp);
 
 
 
     //debug tool- good for determining errors
     // echo "Error: ".mysql_errno()."; error description: ".mysql_error()."";


        
      


    mysql_close($db);
    }  // end of else condition

?>

</body>
</html>