Difference between revisions of "CSC220 Homework 7 Solution 2010"

From dftwiki3
Jump to: navigation, search
 
(No difference)

Latest revision as of 15:25, 17 November 2010

--D. Thiebaut 19:57, 17 November 2010 (UTC)


CSC220 Hw7.png



hw7b.htm

<html>
<!--
hw7b.htm
Alex Cheng (220a-ag)
11/11/10
This is a form to upload kml data.  It sends the information
to hw7b.php.
-->
<head>
<link href='hw7.css' rel='stylesheet' type='text/css' />
<script language="JavaScript" type="text/javascript">
   function checkForm() {
   if (document.kml.email.value == "") {
     alert("Please enter your email address.");
     kml.email.focus();
     return false;
   } 
   if (document.kml.uploaded.value == "") {
     alert("Please select a file to upload.");
     kml.uploaded.focus();
     return false;
   }
   return true;
 }
 // -->                                                                                                        
</script>
</head>

<body>

<div id="title3">KML Data</div>
<div id="title4">Form</div>

<div id="form">

<h3>Please fill out all fields in the form.</h3>

<form enctype ="multipart/form-data" name="kml" action="hw7b.php" 
          method="POST" onsubmit="return checkForm(this)">
<p>File name: <input name="uploaded" type="file" />

<p>Email: <input input type="text" name="email" size="40">
   
<p><input type="submit" value="Upload" />

</form>

<p><a href="hw7index.htm">Return to index</a>

</div>
</body>

</html>

hw7b.php

<?php

/*
hw7b.php
Alex Cheng (220a-ag)
11/10/10
This php page processes the information entered on hw7b.htm.
It enters the information into the kmldata table.
Base program written by Dominique Thiebaut.
*/  

// print html head
function printHtmlHead() {
?>
<html>
<link href='hw7.css' rel='stylesheet' type='text/css' />
<div id="title3">Upload</div>
<div id="title4">Results</div>

<div id="form">

<h3>Here are the results of your upload:</h3>

<body>

<?php
}

// print html tail
function printHtmlTail() {
?>

</body>
</html>
<?php
}

// upload file
function uploadFile( ) {

  // connection
  require("accessinfo.php");
  $connection = mysql_connect($hostName, $userName, $password);
  if (!$connection) {
    die("Not connected: ".mysql_error());
  }

  $db = mysql_select_db($database, $connection);
  if (!$db) {
    die("Can\'t use db: ".mysql_error());
  }

  // get form values
  $originalName = $_FILES['uploaded']['name'];
  $tmp_name     = $_FILES['uploaded']['tmp_name'];
  $destination  = "uploads/" . $originalName;
  $email = $_REQUEST["email"];
  
  // figure out user's id
  $safeEmail = mysql_real_escape_string( $email );
  $idQuery = sprintf("SELECT `userId` from `userInfo` WHERE `email`='" . $safeEmail . "'" );
  $idResult = mysql_query($idQuery, $connection);
  if (!$idResult) {
    die("Invalid query: ".mysql_error());
  }
  while ($row = mysql_fetch_assoc($idResult)) {
    $userId = $row["userId"];
  }
  // if there's no id, then user is not registered
  // else, print confirmation for user
  if ($userId == "") {
    echo "You are not yet registered. You must be registered to upload a file.<br />
<a href='hw7.php'>Go to registration form</a>.";
  } else {
    echo "Thank you.  <br />You have provided the following email: " 
              . $email . " <br />and uploaded the following file: ".$originalName;
    $text = file_get_contents( $tmp_name );
    
    // insert data into kmldata
    $safeText = addslashes( $text );
    mysql_query("INSERT into `kmldata` (`userId`, `data`) VALUES ('".$userId."', '" . $safeText . "')");
  }
}

// do functions :)
printHtmlHead();
uploadFile();
printHtmlTail();

?>

<p><a href="hw7b.htm">Return to upload form</a>
<br><a href="hw7index.htm">Return to index</a>

</div>

</body>

</html>

hw7.css

/*
hw7.css
Alex Cheng (220a-ag)
11/10/10
A CSS file to give all my homework pages formatting.
*/
@charset "UTF-8";

body {
        font-family: Georgia, Times New Roman, serif;
        background-image: url("http://hal.hampshire.edu/~ram/riceWallpaper3.jpg");
}

a:link {
    text-decoration: none;
    color: #000000;
}

a:visited {
    text-decoration: none;
    color: #000000;
}

a:hover {
    text-decoration: underline;
}

#indexpage {
        min-width: 900px;
}

#title1 {
        min-width: 500px;
        position: absolute;
        margin-left: 50px;
        font-size: 100px;
        margin-top: 100px;
}

#title2 {
        min-width: 600px;
        position: absolute;
        margin-left: 85px;
        font-size: 100px;
        margin-top: 160px;
}

#title3 {
        min-width: 500px;
        position: absolute;
        margin-left: 50px;
        font-size: 50px;
        margin-top: -10px;
}

#title4 {
        min-width: 400px;
        margin-left: 75px;
        font-size: 50px;
        position: relative;
        top: 20px;
}

#midpage {
        min-width: 900px;
        position: absolute;
        font-size: 50px;
        margin-top: 300px;
}

#form {
        min-width: 900px;
	margin-left: 100px;
        position: absolute;
        font-size: 17px;
        margin-top: 20px;
}

hw7index.htm

<html>
<!--
hw7index.htm
Alex Cheng (220a-ag)
11/12/10
This is an index page for hw7 that connects all the pages
I created for this assignment (i.e. a regular registration
page that uploads to userInfo, and a kml data upload page
that uploads to kmldata. 
-->
<head><title>CSC 220, Homework 7</title>
<link href='hw7.css' rel='stylesheet' type='text/css' />
</head>

<body>
<div id="indexpage">

<div id="title1">CSC 220</div>
<div id="title2">Project</div>

<div id="midpage">

<table cellpadding="50" align="center">
<tr><td></td>
<td><font size="15">
<a href="hw7.php">Register</a></font></td>
<td></td>
<td><font size="15">
<a href="hw7b.htm">Upload KML</a></font></td></tr>
</table>

</div>

</div>
</body>

</html>

hw7.php

<html>

<!-- 
hw7.php
Alex Cheng (220a-ag)
11/10/10
This program is a form that takes in information and sends 
it to hw7process.php to be processed.
-->

<head>
<title>CSC 220, Homework 7 Form</title>
<meta http-equiv="Pragma" content="no-cache">
<link href='hw7.css' rel='stylesheet' type='text/css' />
<!-- checks that email field is not empty -->
<script language="JavaScript" type="text/javascript">
<!--
   function checkForm() {
   if (document.userInfo.email.value == "") {
     alert( "Please enter your email address." );
     userInfo.email.focus();
     return false;
   }
   return true ;
 }
 // -->
</script>
</head>

<div id="title3">Registration</div>
<div id="title4">Form</div>

<div id="form">
   <h3>Please fill out all fields in the form.</h3>

<?php 
   // connection
   require("accessinfo.php");
$connection = mysql_connect($hostName, $userName, $password);

if (!$connection) {
  die("Not connected: ".mysql_error());
}

$db = mysql_select_db($database, $connection);
if (!$db) {
  die("Can\'t use db: ".mysql_error());
}

// start form
echo '<form name="userInfo" action="hw7process.php" method="post" onsubmit="return checkForm(this)">';
// email
echo '<p>Email: <input type="text" name="email" size="40">';

// affiliation
$affQuery = sprintf("SELECT * from `affiliation`");
$affResult = mysql_query($affQuery, $connection);
if (!$affResult) {
  die("Invalid query: ".mysql_error());
} 
echo '<p>Affiliation: <select name="affId">';
while ($row = mysql_fetch_assoc($affResult)) {
  echo '<option value="'.$row["affId"].'">'.$row["affName"].'</option>';
}
echo '</select>';

// school 
$schoolQuery = sprintf("SELECT * from `schools`");
$schoolResult = mysql_query($schoolQuery, $connection);
if (!$schoolResult) {
  die("Invalid query: ".mysql_error());
}
echo '<p>School: <select name="schoolId">';
while ($row = mysql_fetch_assoc($schoolResult)) {
  echo '<option value="'.$row["schoolId"].'">'.$row["schoolName"].'</option>';
}
echo '</select>';

// housing area 
$houseQuery = sprintf("SELECT * from `housingArea`");
$houseResult = mysql_query($houseQuery, $connection);
if (!$houseResult) {
  die("Invalid query: ".mysql_error());
}
echo '<p>Housing Area: <select name="houseId">';
while ($row = mysql_fetch_assoc($houseResult)) {
  echo '<option value="'.$row["houseId"].'">'.$row["housingArea"].'</option>';
}
echo '</select>';

// major 1
$majorQuery = sprintf("SELECT * from `majors`");
$majorResult = mysql_query($majorQuery, $connection);
if (!$majorResult) {
  die("Invalid query: ".mysql_error());
}
echo '<p>Major: <select name="majorId">';
while ($row = mysql_fetch_assoc($majorResult)) {
  echo '<option value="'.$row["majorId"].'">'.$row["majorName"].'</option>';
}
echo '</select>';

// major 2
$major2Query = sprintf("SELECT * from `majors`");
$major2Result = mysql_query($major2Query, $connection);
if (!$major2Result) {
  die("Invalid query: ".mysql_error());
}
echo '<p>Second Major: <select name="majorId2">';
while ($row = mysql_fetch_assoc($major2Result)) {
  echo '<option value="'.$row["majorId"].'">'.$row["majorName"].'</option>';
}
echo '</select>';

// phone id 
$phoneQuery = sprintf("SELECT * from `phones`");
$phoneResult = mysql_query($phoneQuery, $connection);
if (!$phoneResult) {
  die("Invalid query: ".mysql_error());
}
echo '<p>Phone: <select name="phoneId">';
while ($row = mysql_fetch_assoc($phoneResult)) {
  echo '<option value="'.$row["phoneId"].'">'.$row["phoneName"].'</option>';
}
echo '</select>';

// interest id
$interestQuery = sprintf("SELECT * from `interests`");
$interestResult = mysql_query($interestQuery, $connection);
if (!$interestResult) {
  die("Invalid query: ".mysql_error());
}
echo '<p>Interest: <select name="interestId">';
while ($row = mysql_fetch_assoc($interestResult)) {
  echo '<option value="'.$row["interestId"].'">'.$row["interestName"].'</option>';
}
echo '</select>';

// submit
echo '<p><input type="reset" value="Reset" />  <input type="submit" value="Submit">';
?>

<p><a href="hw7index.htm">Return to index</a>

</div>

</html>

hw7process.php

<html>

<!--
hw7process.php
Alex Cheng (220a-ag)
11/10/10
This page processes the information entered on hw7.php and 
enters it into the userInfo table.  Also displays the 
information.
-->

<head>
<title>CSC 220, Homework 7: Form Processing</title>
<meta http-equiv="Pragma" content="no-cache">
<link href='hw7.css' rel='stylesheet' type='text/css' />
</head>

<body>

<div id="title3">Registration</div>
<div id="title4">Results</div>

<div id="form">

   <h3>Here are the results of your registration:</h3>

<?php
   // connection
   require("accessinfo.php");
$connection = mysql_connect($hostName, $userName, $password);

if (!$connection) {
  die("Not connected: ".mysql_error());
}

$db = mysql_select_db($database, $connection);
if (!$db) {
  die("Can\'t use db: ".mysql_error());
}

// get values from form
$email = $_REQUEST["email"];
$affId = $_REQUEST["affId"];
$schoolId = $_REQUEST["schoolId"];
$housingArea = $_REQUEST["houseId"];
$majorId = $_REQUEST["majorId"];
$majorId2 = $_REQUEST["majorId2"];
$phoneId = $_REQUEST["phoneId"];
$interestId = $_REQUEST["interestId"];
$alreadyReg = false;

// check if user is already registered
$safeEmail = mysql_real_escape_string( $email );
$findQuery = sprintf("SELECT `email` from `userInfo` WHERE `email`='" . $safeEmail . "'");
$findResult = mysql_query($findQuery, $connection);
if (!$findQuery) {
  die("Invalid query: ".mysql_error());
}
while ($row = mysql_fetch_assoc($findResult)) {
  if ($row["email"] == $email) {
    $alreadyReg = true;
  } else {
    $alreadyReg = false;
  }
}

// insert information into db
if (($email != null) && ($alreadyReg == false)) {
  $safeEmail = mysql_real_escape_string( $email ); 
  mysql_query("INSERT into `userInfo` (`email`, `schoolId`, `housingArea`, 
                     `affId`, `majorId`, `majorId2`, `phoneId`, `interestId`) 
                     VALUES ('$safeEmail', '$schoolId', '$housingArea', '$affId', '$majorId', 
                                  '$majorId2', '$phoneId', '$interestId')");
  echo "Your registration was successful!";
  echo "<br /><b>You provided the following email: </b>" . $email;
} else if ($alreadyReg == true) {
  echo "You have already registered!";
} else {
  echo "You did not provide an email!";
}

// print information
if ($alreadyReg == false) {
  // affiliation
  $affQuery = sprintf("SELECT `affName` from `affiliation` WHERE `affId`='".$affId."'");
  $affResult = mysql_query($affQuery, $connection);
  if (!$affResult) {
    die("Invalid query: ".mysql_error());
  }
  echo '<br /><b>Affiliation: </b>';
  while ($row = mysql_fetch_assoc($affResult)) {
    echo $row["affName"];
  }
  
  // school 
  $schoolQuery = sprintf("SELECT `schoolName` from `schools` WHERE `schoolId`='".$schoolId."'");
  $schoolResult = mysql_query($schoolQuery, $connection);
  if (!$schoolResult) {
    die("Invalid query: ".mysql_error());
  }
  echo '<br /><b>School: </b>';
  while ($row = mysql_fetch_assoc($schoolResult)) {
    echo $row["schoolName"];
  }


  // housing area
  $houseQuery = sprintf("SELECT `housingArea` from `housingArea` WHERE `houseId`='".$housingArea."'");
  $houseResult = mysql_query($houseQuery, $connection);
  if (!$houseResult) {
    die("Invalid query: ".mysql_error());
  }
  echo '<br /><b>Housing Area: </b>';
  while ($row = mysql_fetch_assoc($houseResult)) {
    echo $row["housingArea"];
  }
  
  // first major
  $majorQuery = sprintf("SELECT `majorName` from `majors` WHERE `majorId`='".$majorId."'");
  $majorResult = mysql_query($majorQuery, $connection);
  if (!$affResult) {
    die("Invalid query: ".mysql_error());
  }
  echo '<br /><b>Major: </b>';
  while ($row = mysql_fetch_assoc($majorResult)) {
    echo $row["majorName"];
  }
  
  // second major
  $major2Query = sprintf("SELECT `majorName` from `majors` WHERE `majorId`='".$majorId2."'");
  $major2Result = mysql_query($major2Query, $connection);
  if (!$major2Result) {
    die("Invalid query: ".mysql_error());
  }
  echo '<br /><b>Second Major: </b>';
  while ($row = mysql_fetch_assoc($major2Result)) {
    echo $row["majorName"];
  }
  
  // phone type
  $phoneQuery = sprintf("SELECT `phoneName` from `phones` WHERE `phoneId`='".$phoneId."'");
  $phoneResult = mysql_query($phoneQuery, $connection);
  if (!$phoneResult) {
    die("Invalid query: ".mysql_error());
  }
  echo '<br /><b>Phone: </b>';
  while ($row = mysql_fetch_assoc($phoneResult)) {
    echo $row["phoneName"];
  }
  
  // interests
  $interestQuery = sprintf("SELECT `interestName` from `interests` WHERE `interestId`='".$interestId."'");
  $interestResult = mysql_query($interestQuery, $connection);
  if (!$interestResult) {
    die("Invalid query: ".mysql_error());
  }
  echo '<br /><b>Interests: </b>';
  while ($row = mysql_fetch_assoc($interestResult)) {
    echo $row["interestName"];
  }
}  
 
// links
echo '<p><a href="hw7.php">Return to form</a>';
echo '<br><a href="hw7index.htm">Return to index</a>';  

?>

</div>

</body>

</html>