<?php include("../header_test.php") ?>
<?php include("../menu_test.php") ?>
		<div id="content_main">
			<div id="sectionhead">
                OCEAN PICTURE OF THE DAY (OPOD)
			</div>
<?php include("../menu.php") ?>
	    <div id="right_col">
            <div id="content">
            <table border="0" cellpadding="16" cellspacing="0" width="780" bgcolor="white">
                <tr>
                    <td><center><h1>Ocean Picture of the Day (OPOD)<br />
                </tr>
                <tr>
                    <td><div align="center"><strong>OPOD Photo Submission</strong></div></td>
                </tr>
                <tr>
                    <td></td>
                </tr>
                <tr>
                    <td>
								<?php
									$image_folder = "http://www.theoceanproject.org/opod/upload";
									// Upload the image... 
									if ($_FILES['upload']['size'] < 1000000000) {
										if (isset($_REQUEST['firstName'])) {
											if ($_FILES['upload']['error'] > 0) {
												echo "Return Code: " . $_FILES['upload']['error'] . "<br />";
											} else { 
												if (file_exists("upload/".$_FILES['upload']['name'])) {
												   echo "<br />";
												   echo '<span class="Exists">'."upload/".$_FILES['upload']['name'].' image file already exists. Please try again with a different filename'.'</span>' ;
												   echo "<br />";
												} else {
													if ($_FILES['upload']['name'] != null) {
														$target = "upload/";
														$target = $target.basename( $_FILES['upload']['name']) ;
														$ok=1;													
														if (move_uploaded_file($_FILES['upload']['tmp_name'], $target)) {
															echo "<center><img height=375  src=http://www.theoceanproject.org/opod/upload/".$_FILES['upload']['name']." id='name' /></center>"; 
															echo "<center><h2>Successful Upload: ".basename($_FILES['upload']['name'])."</h2></center>";
															echo "<center>Your picture has been submitted for review. You will be notified via e-mail when your picture is going to be used as the OPOD. Thank you so much for your participation.</center>";
															echo "<p> </p>";
														} else {
															echo "Sorry, there was a problem uploading your file.";
														}
													} else {
														echo "Sorry, problem uploading your file: file name is null";
													}											   

													//Connect To Database
													$hostname='mysql27.secureserver.net';
													$username='oceanweb';
													$password='password';
													$dbname='oceanweb';
													$incoming=$_REQUEST['incoming'];
													
													//connect to database
													$con = mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database!');
													//select - use this database 
													mysql_select_db($dbname);
													
													//add information to the following database tables
													$firstName=$_REQUEST['firstName']; //This value has to be the same as in the HTML form file
													$lastName=mysql_real_escape_string($_REQUEST['lastName']); //This value has to be the same as in the HTML form file
													$Email=mysql_real_escape_string($_REQUEST['Email']); //This value has to be the same as in the HTML form file
													$sql1="INSERT INTO user (first,last,email) VALUES ('$firstName','$lastName','$Email')"; //form_data is the name of the MySQL table where the form data will be saved.
													
													$City=mysql_real_escape_string($_REQUEST['City']); //This value has to be the same as in the HTML form file
													$State=mysql_real_escape_string($_REQUEST['State']); //This value has to be the same as in the HTML form file
													$Type=mysql_real_escape_string($_REQUEST['Type']); //This value has to be the same as in the HTML form file
													$currentdatetime= date("Y-m-d H:i:s");
													$location= mysql_real_escape_string($_REQUEST['location']);
													
													//works except type
													$sql2="INSERT INTO pic SET size='".$_FILES['upload']['size']."', date='$currentdatetime',town='$City',state='$State', image_type='$Type' , name='".$_FILES['upload']['name']."', location='$location'";
													
													$textarea=mysql_real_escape_string($_REQUEST['textarea']); //This value has to be the same as in the HTML form file
													$sql3="INSERT INTO submits (caption) VALUES ('$textarea')"; //form_data is the name of the MySQL table where the form data will be saved.												
													if (!mysql_query($sql1,$con)) {
													 die('Error: ' . mysql_error()); 
													} 
													
													if (!mysql_query($sql2,$con)) {
													 die('Error: ' . mysql_error()); 
													} 
													
													if (!mysql_query($sql3,$con)) {
													 die('Error: ' . mysql_error()); 
													} 
													
													echo '<br />';
													
													$result = mysql_query("SELECT MAX(id) AS id from submits");
													
													if($row = mysql_fetch_array($result)) {
														$id = $row['id'];
														$sql4 = mysql_query("UPDATE submits SET picture_id=".$id." WHERE id=".$id);
														$sql5 = mysql_query("UPDATE submits SET user_id=".$id." WHERE id=".$id);
														
														if (!mysql_query($sql4,$con)) {
														 die(); 
														} 
																												 
														if (!mysql_query($sql5,$con)) {
														 die(); 
														} 
													}
													
													mysql_close($con);
												}
											}
										}
									} else  {
									  echo '<span class="Error">'."Invalid File".'</span>'."<br />";
									}
                                ?>
                        </td>
                    </tr>
                    <tr>
                    	<td style="font-size: 10px">
                        	<center><p style="font-size:12px"><a href="index.php" onClick="return (false);">View Today's OPOD Record</a></p>
                      		<p style="font-size:12px"><a href="upload_form.php" onClick="return (false);">Submit Another OPOD Picture</a></p></center>
                    	</td>
            		</tr>
            	</table>
        	</div>
        </div>
        <div id="footer" style="left:464px;top:712px">
        <table>
            <tr>
                <td style="background:transparent">
<?php include("../footer_div.php") ?>
</div>
</body>
</html>