title: MySQL Database - Transferring inputted data to a text file (PHP, HTML, SQL)
body: <p>I just can't seem to figure out the logic to write the inputted information (contact form) to a text file within the localhost.</p>
  
  <p>I am uploading all of this information on my school's server. If I please could get some help. Truly can't figure this part out.</p>
  
  <blockquote>
    <p>HTML CODE:</p>
  </blockquote>
  
  <pre><code>    &lt;form action="Registered.php" method="post"&gt;
       &lt;p&gt;
          &lt;label&gt;First Name:&lt;/label&gt;
          &lt;input name="fName" type="text"&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;label&gt;Last Name:&lt;/label&gt;
          &lt;input name="lName" type="text"&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;label&gt;Address:&lt;/label&gt;
          &lt;input name="address" type="text"&gt;        
      &lt;/p&gt;
      &lt;p&gt;
          &lt;label&gt;State:&lt;/label&gt;
          &lt;select name="statedropdown"&gt;
  &lt;option value="Al"&gt; Al &lt;/option&gt;
  &lt;option value="AK"&gt; AK &lt;/option&gt;
  &lt;option value="AS"&gt;AS&lt;/option&gt;
  &lt;option value="AR"&gt;AR&lt;/option&gt;
  &lt;option value="CA"&gt;CA&lt;/option&gt;
  &lt;option value="CO"&gt;CO&lt;/option&gt;
  &lt;option value="CT"&gt;CT&lt;/option&gt;
  &lt;option value="DE"&gt;DE&lt;/option&gt;
  &lt;option value="DC"&gt;DC&lt;/option&gt;
  &lt;option value="FL"&gt;FL&lt;/option&gt;
  &lt;option value="GA"&gt;GA&lt;/option&gt;
  &lt;option value="HI"&gt;HI&lt;/option&gt;
  &lt;option value="ID"&gt;ID&lt;/option&gt;
  &lt;option value="IL"&gt;IL&lt;/option&gt;
  &lt;option value="IN"&gt;IN&lt;/option&gt;
  &lt;option value="IA"&gt;IA&lt;/option&gt;
  &lt;option value="KS"&gt;KS&lt;/option&gt;
  &lt;option value="KY"&gt;KY&lt;/option&gt;
  &lt;option value="LA"&gt;LA&lt;/option&gt;
  &lt;option value="ME"&gt;ME&lt;/option&gt;
  &lt;option value="MD"&gt;MD&lt;/option&gt;
  &lt;option value="MA"&gt;MA&lt;/option&gt;
  &lt;option value="MI"&gt;MI&lt;/option&gt;
  &lt;option value="MN"&gt;MN&lt;/option&gt;
  &lt;option value="MS"&gt;MS&lt;/option&gt;
  &lt;option value="MO"&gt;MO&lt;/option&gt;
  &lt;option value="MT"&gt;MT&lt;/option&gt;
  &lt;option value="NE"&gt;NE&lt;/option&gt;
  &lt;option value="NV"&gt;NV&lt;/option&gt;
  &lt;option value="NH"&gt;NH&lt;/option&gt;
  &lt;option value="NJ"&gt;NJ&lt;/option&gt;
  &lt;option value="NM"&gt;NM&lt;/option&gt;
  &lt;option value="NY"&gt;NY&lt;/option&gt;
  &lt;option value="NC"&gt;NC&lt;/option&gt;
  &lt;option value="ND"&gt;ND&lt;/option&gt;
  &lt;option value="OH"&gt;OH&lt;/option&gt;
  &lt;option value="OK"&gt;OK&lt;/option&gt;
  &lt;option value="OR"&gt;OR&lt;/option&gt;
  &lt;option value="PA"&gt;PA&lt;/option&gt;
  &lt;option value="RI"&gt;RI&lt;/option&gt;
  &lt;option value="SC"&gt;SC&lt;/option&gt;
  &lt;option value="SD"&gt;SD&lt;/option&gt;
  &lt;option value="TN"&gt;TN&lt;/option&gt;
  &lt;option value="UT"&gt;UT&lt;/option&gt;
  &lt;option value="VT"&gt;VT&lt;/option&gt;
  &lt;option value="VA"&gt;VA&lt;/option&gt;
  &lt;option value="WA"&gt;WA&lt;/option&gt;
  &lt;option value="WV"&gt;WV&lt;/option&gt;
  &lt;option value="WI"&gt;WI&lt;/option&gt;
  &lt;option value="WY"&gt;WY&lt;/option&gt;
  &lt;/select&gt;       
      &lt;/p&gt;
      &lt;p&gt;
          &lt;label&gt;ZIP Code:&lt;/label&gt;
          &lt;input name="zip" required="required" placeholder="12345" type="text"&gt;        
      &lt;/p&gt;
      &lt;p&gt;
          &lt;label&gt;Email:&lt;/label&gt;
          &lt;input name="email" required="required" placeholder="fake@email.com" type="email"&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;label&gt;Phone Number:&lt;/label&gt;
          &lt;input name="phone" required="required" placeholder="912-555-1234" type="text"&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;input value="Submit" type="submit"&gt;
          &lt;input type="reset" value="Reset"&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;td align="center"&gt;&lt;a href="http://web-students.armstrong.edu/~tp2283/FormData.txt"&gt; View contacts in database &lt;/a&gt;&lt;/td&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;td align="center"&gt;&lt;a href="http://web-students.armstrong.edu/~tp2283/contactsFile.htm"&gt; View contacts in file &lt;/a&gt;&lt;/td&gt;
      &lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <blockquote>
    <p>PHP CODE:</p>
  </blockquote>
  
  <pre><code>    &lt;html&gt;    
     &lt;head&gt;
        &lt;title&gt; Thank You &lt;/title&gt;
     &lt;/head&gt;
     &lt;body&gt;
     &lt;?php
              $username="YYYYYYYY";
              $password="XXXXXXXX";
              $database="ZZZZZZZZ";
  
                  #declare variables
                  $fName = $_POST['fName'];
                  $lName = $_POST['lName'];
                  $address = $_POST['address'];
                  $statedropdown = $_POST['statedropdown'];
                  $zip = $_POST['zip'];
                  $phone = $_POST['phone'];
                  $email = $_POST['email'];
                  $DOCUMENT_ROOT = $SERVER['DOCUMENT_ROOT'];
  
                  mysql_connect(localhost,$username,$password);
                  mysql_select_db($database) or die( "Unable to select database");
                  $query = "SELECT * FROM contacts";
                  $result = mysql_query($query);
                  $num = mysql_num_rows($result);
  
                  mysql_close();
  ?&gt;
        &lt;h1 align = "center"&gt; Thanks for Registering! &lt;/h1&gt; &lt;br /&gt;&lt;br /&gt;
           &lt;p align = "center"&gt; Your information is: &lt;/p&gt;
              &lt;table align = "center"&gt;
                 &lt;tr&gt;
                    &lt;td&gt; First Name: &lt;/td&gt;
                    &lt;td&gt; &amp;nbsp &lt;/td&gt; 
                    &lt;td&gt; &lt;?php echo $fName ?&gt; &lt;/td&gt;
                 &lt;/tr&gt;
                 &lt;tr&gt;
                    &lt;td&gt; Last Name: &lt;/td&gt;
                    &lt;td&gt; &amp;nbsp &lt;/td&gt; 
                    &lt;td&gt; &lt;?php echo $lName ?&gt; &lt;/td&gt;
                 &lt;/tr&gt;
                 &lt;tr&gt;
                    &lt;td&gt; Address: &lt;/td&gt;
                    &lt;td&gt; &amp;nbsp &lt;/td&gt; 
                    &lt;td&gt; &lt;?php echo $address ?&gt; &lt;/td&gt;
                 &lt;/tr&gt;
                 &lt;tr&gt;
                    &lt;td&gt; State: &lt;/td&gt;
                    &lt;td&gt; &amp;nbsp &lt;/td&gt; 
                    &lt;td&gt; &lt;?php echo $statedropdown ?&gt; &lt;/td&gt;
                 &lt;/tr&gt;
                 &lt;tr&gt;
                    &lt;td&gt; Zip: &lt;/td&gt;
                    &lt;td&gt; &amp;nbsp &lt;/td&gt; 
                    &lt;td&gt; &lt;?php echo $zip ?&gt; &lt;/td&gt;
                 &lt;/tr&gt;
                 &lt;tr&gt;
                    &lt;td&gt; Telephone: &lt;/td&gt;
                    &lt;td&gt; &amp;nbsp &lt;/td&gt; 
                    &lt;td&gt; &lt;?php echo $phone ?&gt; &lt;/td&gt;
                 &lt;/tr&gt;
                 &lt;tr&gt;
                    &lt;td&gt; E-mail: &lt;/td&gt;
                    &lt;td&gt; &amp;nbsp &lt;/td&gt; 
                    &lt;td&gt; &lt;?php echo $email ?&gt; &lt;/td&gt;
                 &lt;/tr&gt;
              &lt;/table&gt;
  
  &lt;?php
      if($_POST['saveToFile'] == 'Save to File') {
          $outputstring =
          "First Name: $fName
          Last Name: $lName
          Address: $address
          State: $statedropdown
          Zip: $zip
          Telephone: $phone
          Email: $email
          -----------------------\n";
  
          $fp = fopen("FormData.txt", 'a');
          flock($fp, LOCK_EX);
  
          fwrite($fp, $outputstring, strlen($outputstring));
          flock($fp, LOCK_UN);
          fclose($fp);
      }
  ?&gt;
        &lt;p align="center"&gt;&lt;a href="Assignment3.html"&gt; Return to Main Page &lt;/a&gt; &lt;/p&gt;
     &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>If someone could please help me out this would be highly appreciated!</p>