<?php require_once("header.php"); ?>

<h1>About</h1>

<?php 

require_once("../includes/quickmysql.php");

connect();

// if the file has posted to itself
if (isset($_POST['submit'])){

    // store the name of the image in a variable called $image
    // docs for $_FILES http://www.w3schools.com/php/php_file_upload.asp
    $image = $_FILES['image']['name'];
    // just incase there are slashes in the filename
    $filename = stripslashes($image);
   
    
	$title = mysql_real_escape_string($_POST['title']);
	$content = mysql_real_escape_string($_POST['content']);
	
	// piece together the mysql query based on if a file was uploaded or not
	$query = "UPDATE about SET title = '$title', content = '$content' ";
	
	// don't update image field unless a file has been attached
	if ($filename){
	   $query .= ", image = '$image'";
	   // $_FILES['image']['tmp_name'] is the actual temporary file that has been posted
	   // copy copies the temp image to a given location (you can rename the file if you like)
	   $copied = copy($_FILES['image']['tmp_name'], "../images/" . $filename);
	}
	
	$query .= "WHERE id = 0;";
 
    // run the query
	$result = mysql_query($query);
	  
	// if it fails display failure text and if it works display confirmation text
	if ($result){
	  echo "Your information was saved. <br /><br />";
	}else{
	  echo "There was an error saving the information. <br /><br />";
	}
}
  
  $data = getSingleRow("about");

disconnect();
?>


<form action="" method="post" enctype="multipart/form-data">
  Title<br />
  <input type="text" name="title" size="50" value="<?php echo $data["title"]; ?>" /><br /><br />
  Content<br />
  <textarea name="content" cols="50" rows="10"><?php echo $data["content"]; ?></textarea><br /><br />
  
  About Image: <?php echo $data["image"]; ?><br />
  <img src="../images/<?php echo $data["image"]; ?>" /><br />
  <input type="file" name="image"><br /><br />
  <input type="submit" name="submit" value="Save" />
</form>

<?php require_once("footer.php"); ?>