PHP & MySQL – Creating a SQL script for Contact form in PHP/HTML

exception or error:

Okay so let’s start with this, I am not wanting anyone to do my project/homework, I am simply looking for help into finishing this extended project. I am very new to HTML, PHP, & MYSQL.

We are extending a project that we did before. The project before we created a contact form with HTML and PHP, then uploaded it to our school’s server. I will provide you with the code for all the files for the project.

Here is the code for the .html file for the contact form (Assignment3.html):

<html>
    <head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <style type="text/css"> 
    </style>
</head>
<body bgcolor="green">
<form action="Registered.php" method="post" align="center">
</br>
</br>

<label>
    <font size="5">
        <strong>Contact Form</strong>
    </font>
</label>
<p>     
    <strong>First Name:</strong>
         <input type="text" name="fname">
</p>
<p>
    <strong>Last Name:</strong>
         <input type="text" name="lname">
</p>
<p>
    <strong>Address:</strong>
         <input type="text" name="address">
</p>
<p>
    <strong>State:</strong>
         <select name="state">
<option value="Al">Al</option>
<option value="AK">AK</option>
<option value="AS">AS</option>
<option value="AR">AR</option>
<option value="CA">CA</option>
<option value="CO">CO</option>
<option value="CT">CT</option>
<option value="DE">DE</option>
<option value="DC">DC</option>
<option value="FL">FL</option>
<option value="GA">GA</option>
<option value="HI">HI</option>
<option value="ID">ID</option>
<option value="IL">IL</option>
<option value="IN">IN</option>
<option value="IA">IA</option>
<option value="KS">KS</option>
<option value="KY">KY</option>
<option value="LA">LA</option>
<option value="ME">ME</option>
<option value="MD">MD</option>
<option value="MA">MA</option>
<option value="MI">MI</option>
<option value="MN">MN</option>
<option value="MS">MS</option>
<option value="MO">MO</option>
<option value="MT">MT</option>
<option value="NE">NE</option>
<option value="NV">NV</option>
<option value="NH">NH</option>
<option value="NJ">NJ</option>
<option value="NM">NM</option>
<option value="NY">NY</option>
<option value="NC">NC</option>
<option value="ND">ND</option>
<option value="OH">OH</option>
<option value="OK">OK</option>
<option value="OR">OR</option>
<option value="PA">PA</option>
<option value="RI">RI</option>
<option value="SC">SC</option>
<option value="SD">SD</option>
<option value="TN">TN</option>
<option value="UT">UT</option>
<option value="VT">VT</option>
<option value="VA">VA</option>
<option value="WA">WA</option>
<option value="WV">WV</option>
<option value="WI">WI</option>
<option value="WY">WY</option>
</select>
</strong></strong></p>
<strong><strong>
<p>
    <strong>Zip Code:</strong>
         <input type="text" name="zip">
</p>
<p>
    <strong>Phone Number:<strong>
         <input type="text" name="phone">
</strong></strong></p>
<strong><strong>
<p>
    <strong>Email:<strong>
         <input type="text" name="email">
</strong></strong></p>
<strong><strong>
<p>
    <input type="reset" value="Reset">
    <input type="submit" value="Save to database">
    <input type="submit" value="Save to file"></p>
<p>
<a href="">View contacts in database</a>
</p>
<p>
<a href="http://web-students.armstrong.edu/~tp2283/contactsFile.html">View contacts in file</a>
</p>
</strong></strong></strong></strong></strong></strong>
</form>
</body>
</html>

He*re is the code for the .php file (Registered.php):*

<html>    
   <head>
      <title> Thank You </title>
   </head>
   <body bgcolor = "blue">
   <?php
                #declare variables
                $fname = $_POST['fname'];
                $lname = $_POST['lname'];
                $address = $_POST['address'];
                $state = $_POST['state'];
                $zip = $_POST['zip'];
                $phone = $_POST['phone'];
                $email = $_POST['email'];
                $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
    ?>
      <h1 align = "center"> Thanks for Registering! </h1> 
         <p align = "center"> Your information is: </p>
            <table align = "center">
               <tr>
                  <td> First Name: </td>
                  <td> &nbsp </td> 
                  <td> <?php echo $fname ?> </td>
               </tr>
               <tr>
                  <td> Last Name: </td>
                  <td> &nbsp </td> 
                  <td> <?php echo $lname ?> </td>
               </tr>
               <tr>
                  <td> Address: </td>
                  <td> &nbsp </td> 
                  <td> <?php echo $address ?> </td>
               </tr>
               <tr>
                  <td> State: </td>
                  <td> &nbsp </td> 
                  <td> <?php echo $state ?> </td>
               </tr>
               <tr>
                  <td> Zip: </td>
                  <td> &nbsp </td> 
                  <td> <?php echo $zip ?> </td>
               </tr>
               <tr>
                  <td> Telephone: </td>
                  <td> &nbsp </td> 
                  <td> <?php echo $phone ?> </td>
               </tr>
               <tr>
                  <td> E-mail: </td>
                  <td> &nbsp </td> 
                  <td> <?php echo $email ?> </td>
               </tr>
            </table>

<?php
            if($_POST['saveToFile'] == 'Save to File') {
        $outputstring =
        "First Name: $fname
        Last Name: $lname
        Address: $address
        State: $state
        Zip: $zip
        Telephone: $phone
        Email: $email
        -----------------------\n";

        $fp = fopen("$DOCUMENT_ROOT/../home/students/tp2283/public_html/FormData.txt", 'a');
        flock($fp, LOCK_EX);

        fwrite($fp, $outputstring, strlen($outputstring));
        flock($fp, LOCK_UN);
        fclose($fp);
    }
?>
      <p align="center"><a href="Assignment3.html"> Return to Main Page </a> </p>
   </body>
</html>

First off I will say that the form is supposed to save to a text file with all of the contacts. I cannot figure out how to do that. I also have a seperate .html file (contactsFile.html) which is written out contacts. Is there a way to save the newly added contacts to the .html file or at least another text file that incorporates the .html file?

Here is the contactsFile.html code:

<html>
    <head><meta http-equiv = "Content-Type" content="text/html; charset=ISO-8859-1">
        <style><type="text/css"></style>
    </head>
<body bgcolor = "red"><center>
<p>
    <font size="7">
        <strong>Contacts:</strong>
    </font>
</p>
<p>First name: Don</p>
<p>Last name: Juan</p>
<p>Address: 120 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1111</p>
<p>Email: fake1@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Jim</p>
<p>Last name: Piper</p>
<p>Address: 121 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1112</p>
<p>Email: fake2@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Sarah</p>
<p>Last name: Arnold</p>
<p>Address: 122 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1113</p>
<p>Email: fake3@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Bethany</p>
<p>Last name: Hattaway</p>
<p>Address: 123 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1114</p>
<p>Email: fake4@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Jermaine</p>
<p>Last name: Siler</p>
<p>Address: 124 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1115</p>
<p>Email: fake5@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Michael</p>
<p>Last name: Crosby</p>
<p>Address: 125 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1116</p>
<p>Email: fake6@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Jayme</p>
<p>Last name: Collins</p>
<p>Address: 126 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1117</p>
<p>Email: fake7@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Mitchell</p>
<p>Last name: Hudson</p>
<p>Address: 127 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1118</p>
<p>Email: fake8@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Roz</p>
<p>Last name: Wilson</p>
<p>Address: 128 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1119</p>
<p>Email: fake9@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Laura</p>
<p>Last name: Spangenburg</p>
<p>Address: 129 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1120</p>
<p>Email: fake10@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Morris</p>
<p>Last name: Alfred</p>
<p>Address: 130 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1121</p>
<p>Email: fake21@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Peggy</p>
<p>Last name: Williams</p>
<p>Address: 131 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1122</p>
<p>Email: fake22@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Tyler</p>
<p>Last name: Daniel</p>
<p>Address: 132 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1123</p>
<p>Email: fake23@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Sean</p>
<p>Last name: Michaels</p>
<p>Address: 133 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1124</p>
<p>Email: fake24@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Jim</p>
<p>Last name: Heart</p>
<p>Address: 134 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1251</p>
<p>Email: fake25@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Paul</p>
<p>Last name: Bernard</p>
<p>Address: 135 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1126</p>
<p>Email: fake26@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Jessica</p>
<p>Last name: Simpson</p>
<p>Address: 121 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1142</p>
<p>Email: fake42@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Darren</p>
<p>Last name:Lockheart</p>
<p>Address: 142 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1162</p>
<p>Email: fake46@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Michael</p>
<p>Last name: Scott</p>
<p>Address: 1121 Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-1133</p>
<p>Email: fake33@yahoo.com</p>
<p>_____________________________________________</p>
<p>First name: Donald</p>
<p>Last name: Duck</p>
<p>Address: QUACK Street</p>
<p>State: GA</p>
<p>Zip: 31419</p>
<p>Telephone: 478-555-5555</p>
<p>Email: fake555@yahoo.com</p>
<p>_____________________________________________</p>

</center>
<p align="center"><a href="Assignment3.html"> Return to Main Page </a> </p>
</body></html>

To extend the assignment we are supposed to use the same application but this time we are to create a MySQL database to store the information from the forms.

This is something I do not have experience with… I am okay with HTML and some PHP, but do not know how to convert to MySQL or go about that? If someone can point me in the right direction!

Here is an image of my form… everything works besides “Save to Database” and “View Contacts in Database”, as well as the save new contacts to file.
http://i44.tinypic.com/2dqmv7q.jpg
So if someone could give me some input!

Next we are supposed to write an SQL script that you run & generates and populates the appropriate tables for the application with at least 100 contacts. Please provide a brief explanation for each field of the table (or tables) in your DB…

After this is all complete you go through the security measures it took for the web application.

Thanks for the help ahead of time!! Let me know if you could use more information. Look forward to hearing from you

How to solve:

There have always been something I don’t like about our current school system.. I learnt programming by search-‘n’-try-until-you-make-it. I have never had anyone to ask. This have made me pretty good at figuring and finding out and how to do something I don’t know how to do, by myself and with the power of the internet. So I recommend you to start a project by your own, with almost impossible and big goals. Then make sure that you make it. And don’t spend too much time asking other people, find the answer yourself by searching google and testing/playing around with the code, and take a lot of small web based tutorials for a specific ting you want to do, then combine these to this big amazing project.

A simple search for “MySQL” gave me this: http://www.w3schools.com/php/php_mysql_intro.asp which could work as an introduction.

However..

If you have set up your MySQL db (or have the login info), create a table named “notes” with a column named “text”, and then check out this example:

<?php
header('Content-Type: text/html; charset=utf-8');

$dbhost     = "xxx";
$dbuser     = "yyy";
$dbpass     = "zzz";
$dbname     = "xyz";

$connect = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $connect);

function esc($str) {
    $str = strip_tags(stripslashes($str));
    return mysql_real_escape_string($str);
}

// Always use _get instead of $_GET or $_POST for safety reason
function _get($str) {
    if (!isset($_REQUEST[$str])) return null;

    if (is_array($_REQUEST[$str]))
        return array_map_r('esc', $_REQUEST[$str]);
    return esc($_REQUEST[$str]);
}

?>

<!DOCTYPE html>
<html>
<head>
    <title> Hello! </title>
</head>
<body>


<form method="post" action=".">
<textarea id="note" placeholder="Your note..." name="text"></textarea>
<input type="submit" value="Post it"/>

<div>
<?php

$text = _get("text");
if (isset($text)) {
    // Save text
    mysql_query("INSERT INTO notes (text) VALUES ('$text')");
}


// Fetch saved texts
$sql_ret = mysql_query("SELECT * FROM notes ORDER BY id DESC");
if (mysql_num_rows($sql_ret)<1) {
    // Error
} else {
    while($note = mysql_fetch_array($sql_ret)) {
        $text = $note['text'];
        echo "<span>".$text."</span>";
    }
}


?>
</div>

</form>


</body>
</html>

Good Luck!!

Leave a Reply

Your email address will not be published. Required fields are marked *