<?php session_start();
$previous = $_SESSION['previous'];
$_SESSION['previous'] = "register";
echo $previous.'<br>';

  if (isset($_SESSION['userid']))
  { 
	  die('username is already active.');
  }
  if (isset($_SESSION['reg_again']))
 {
	 echo 'USER ALREADY EXISTS. PLEASE CHOOSE ANOTHER USERID.<br><br>';
	 unset($_SESSION['reg_again']);
 }
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>REGISTRATION PAGE</title>

<?php 
if (isset($_POST['userid']))
{
	$con = mysql_connect("localhost","root","programming");
	if (!$con)
	{  die('Could not connect: ' . mysql_error());  }

	mysql_select_db("BOOK_DB", $con);
	$query = sprintf("SELECT USER_ID from PROFILE WHERE USER_ID='%s'; ",$_POST['userid']);
	$result = mysql_query($query,$con);

	if(mysql_num_rows($result)==0)
	  {
		$query = sprintf("insert into PROFILE values('%s','%s','%s','%s');",$_POST['userid'],$_POST['name'],$_POST['address'],$_POST['password']);
		if (!mysql_query($query,$con))
		{
			die('Error: ' .mysql_error());
		}
		$_SESSION['userid'] = $_POST['userid'];
		//send to wherever it came from
		echo '<script type="text/javascript"> document.location.href = "home.php?"; </script>';
		//echo '<script type="text/javascript"> document.location.href = "search_display.php?"; </script>';
	  }
	else{
		$_SESSION['name'] = $_POST['name'];
		$_SESSION['address'] = $_POST['address'];
		$_SESSION['reg_again']='set';
		echo '<script type="text/javascript"> document.location.href = "register.php?"; </script>';	
	    }
}
?>

<script type="text/javascript">
function validate(form)
{
form.submit()
}

function re(form1)
{ 
  if(confirm("Do you want to reset the form"))
  {
  }   
}
</script>

</head>
<body></body>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <label>userid
  <input type="text" name="userid" id="userid" value="ashish" />
  </label>
  <br>
  <label>name
  <input type="text" name="name" id="name" value="Ashish Gupta" />
  </label>
  <br>
  <label>address
  <input type="text" name="address" id="address" value="IIT Kanpur" />
  </label>
  <br>
  <label>password
  <input type="text" name="password" id="password" value="password" />
  </label>
  <p>
    <input type="submit" onClick="validate(this.form)" name="button" id="button" value="Submit" />
    <input name="b" type="button" onClick="re(this.form)" id="b" value="Clear" />
  </p>
</form>

<br><br>  <a href="home.php">HOME</a>  <br><br>

</body>
</html>
