<?php

	$firstName = isset($_POST['firstName']) ? $_POST['firstName'] : "";
	$lastName = isset($_POST['lastName']) ? $_POST['lastName'] : "";
	$fear = isset($_POST['fear']) ? $_POST['fear'] : "";

	$date = date("Y-m-d");
	$error = 0;

	// PRE-MAKE DATABASE
	// go to phpmyadmin
	// login and make a table called "fears" with 5 fields
	// id - make it an INT and autoincrementing and the key
	// firstName - make TEXT
	// lastName - make TEXT
	// fear - make TEXT
	// date - male Date

	// OPEN DATABASE
	// use your username and password
	$db = mysql_connect("localhost", "yourUserName", "yourPassword");
	mysql_select_db("yourDatabase",$db);

	if ($firstName != "") {
		// INPUT TO MYSQL
		// create SQL query to insert any names and fears into the "fears" table
		$query = "INSERT INTO fears (firstName, lastName, fear, date) VALUES ('$firstName', '$lastName', '$fear', '$date')";
		// use PHP function to execute query.  We use this function every time we
		// want to access the database - for instance to add, delete or change something
		$result = mysql_query($query,$db);
		if (!$result) {
			$error = "Sorry could not record fear";
		}
	}

	// OUTPUT TO FLASH
	// this is how we handle sending variables back to Flash
	// note that all name=value pairs are separated by &
	$query = "SELECT * FROM fears";
	$result = mysql_query($query,$db);
	$total = mysql_num_rows($result);
	$output = "";
	if ($result) {
		$i=0;
		while ($myrow = mysql_fetch_array($result)) {
			$i++;
			$output .= "&firstName".$i."=".urlencode($myrow['firstName']).
					   "&lastName".$i."=".urlencode($myrow['lastName']).
					   "&fear".$i."=".urlencode($myrow['fear']);
		}
	} else {
		$error = "Sorry could not access fears";
	}

	// send the data to Flash using the URL encoded format
	echo "total=".urlencode($total)."&error=".urlencode($error).$output;


?>