<html>
<? include '../head.php' ?>
<body>
<? $navmode = "database"; include '../nav.php'; ?>

<div id="content">
<?php

$database="crystals";

if(array_key_exists('submit',$_POST))
{
    $id = $_POST['id'];
    
    $new_name = $_POST['name'];
    $new_formula = $_POST['formula'];
    $new_description = $_POST['description'];
	$new_sgnum = $_POST['sgnum'];
    $new_atoms = $_POST['atoms'];
    
	
    $username="crystal_write";
    //$password="cweb785";
    $password=$_POST['pass'];

    mysql_connect(localhost,$username , $password);
    @mysql_select_db($database) or die( "Unable to select database");
        
	if ($id == "new")
	{
		$query = "INSERT INTO crystals (name, formula, description, sgnum, atoms) VALUES
			('$new_name', '$new_formula', '$new_description', '$new_atoms', '$new_atoms')";
		$result=mysql_query($query);
		echo $results;
		$id=mysql_insert_id();
		echo "<h2>Crystal Added with id : $id</h2>";
	}
	else {
		$query= "UPDATE crystals SET
			`name`='$new_name',
			`formula`='$new_formula',
			`description`='$new_description',
			`sgnum` = '$new_sgnum',
			`atoms` = '$new_atoms'
			WHERE `id` = '$id'";
		$result=mysql_query($query) or die(mysql_error());
		echo "<h2>Changes Made to : $id</h2>";
	}
    mysql_close();

}
else
{
    echo "<h2>Make Changes to :</h2>";
    $id = $_GET['id'];
}

if ($_GET['action'] == "new")
{
	echo "<h2>Make New Crystal</h2>";
	$id = "new";
}

if ($_GET['action'] != "new")
{
	$username="www";
	mysql_connect(localhost,$username);
	@mysql_select_db($database) or die( "Unable to select database");
	
	$query= "SELECT * FROM crystals WHERE id=$id";
	
	$result=mysql_query($query);
	
	$current_name = mysql_result($result,0,"name");
	$current_formula = mysql_result($result,0,"formula");
	$current_description = mysql_result($result,0,"description");
	$current_sgnum = mysql_result($result,0,"sgnum");
	$current_atoms = mysql_result($result,0,"atoms");

	mysql_close();
}
?>


<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ; ?>" enctype="multipart/form-data">

<ul>
    <li>
		ID: <?php echo $id ?> 
		<input type="hidden" name="id" value="<?php echo $id ?>" />
	</li>
    
    
    <li>
		Name:
		<input type="text" name="name" size="30" value="<?php echo $current_name ?>"/>
	</li>
    
    <li>
		Formula:
		<input type="text" name="formula"
			size="25"
			value="<?php echo $current_formula ?>"/>
	</li>
    
    <li>Description:<br/>
		<textarea cols="40"	rows="6"
			type="text"
			name="description"><?php echo $current_description ?></textarea>
	</li>
	
	<li>
		SpaceGroup: 
		<input name="sgnum" size="5" type="text" value="<?php echo $current_sgnum ?>"/>
	</li>
	
	<li>Atom Data:<br/>
		 <textarea cols="20"
		 	rows="10" type="text"
		 	name="atoms"><?php echo $current_atoms ?></textarea>
	</li>
</ul>
	<p>password<input type="password" name="pass" size="8"/>
	<input type="submit" name="submit" value="Save Changes"/></p>

</form>

<form method="GET" action="<?php echo $_SERVER['PHP_SELF'] ; ?>">
	<input type="hidden" name="id" value="<?php echo $id ?>" />
	<input type="submit" name="reset" value="Reset"/>
</form>

<a href="crystal.php?id=<?php echo $id ?>">[View Crystal]</a>

</div>
</body>
</html>