When I am trying to insert the data into a table for the registration details I am getting the error two errors:
1. on line 39 Parse error: syntax error,
2. unexpected T_ELSE on line 39.
What my idea is to check the username which is already in the database and prompt the user that the username is already taken. else it should insert the data into my userdetails table in MySQL.
Kindly let me know where I am going wrong..
Please find my PHP files for register.php below is the code:
Code: Select all
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Sanskar - Life with a difference</title>
<link rel="stylesheet" href="style.css" type="text/css" charset="utf-8" />
<script language = "Javascript">
function Validate()
{
if (document.form1.firstname.value == '')
{
alert('Please fill in your first name');
return false;
}
if (document.form1.lastname.value == '')
{
alert('Please fill in your last name');
return false;
}
if (document.form1.username.value == '')
{
alert('Please fill in your desired username');
return false;
}
if (document.form1.password.value == '')
{
alert('Please fill in your desired password!');
return false;
}
if (document.form1.cpassword.value == '')
{
alert('Please fill in your password again for confirmation!');
return false;
}
if (document.form1.password.value != document.form1.cpassword.value)
{
alert("The two passwords are not identical! "+
"Please enter the same password again for confirmation");
return false;
}
if (document.form1.email.value == '')
{
alert('Please fill in your Email Address');
return false;
}
if (document.form1.location.value == '')
{
alert('Please fill in your Email Address');
return false;
}
return true;
}
</script>
</head>
</head>
<body>
<div id="outer">
<div id="wrapper">
<div id="nav">
<div class="clear"></div>
</div>
<div id="head">
<div id="head-left"></div>
<div id="head-right"></div>
<div id="head-1"></div>
<h1><span class="logo"><span class="top">Sanskar</span><span class="gadgets">life with a difference</span></span></h1>
</div>
<div id="head-2"></div>
<div id="login">
<div id="login-bot">
<div id="login-box">
<h2 class="login"><em>User</em> Registeration Page</h2>
<form name='form1' method="post" action="insert_ac.php" onsubmit="return Validate();">
<div id="login-username">
<div><label for="firstname">First Name</label>: <input type="text" name="firstname" id="firstname"></div>
<div><label for="lastname">Last Name</label>: <input type="text" name="lastname" id="lastname"></div>
<div><label for="username">Username</label>: <input type="text" name="username" id="username"></div>
<div><label for="password">Password</label>: <input type="password" name="password" id="password"></div>
<div><label for="cpassword">C Password</label>: <input type="password" name="cpassword" id="cpassword"></div>
<div><label for="email">Email</label>: <input type="text" name="email" id="email"></div>
<div><label for="location">Location</label>: <input type="text" name="location" id="location"></div>
</div>
<div class="clear">
<div id="login-button">
<input type="image" src="images/btn_login.gif" name="l" value="h"/>
</div>
</div>
</form>
</div>
<div id="login-welcome">
<div>
<h2>Welcome</h2>
<p>Don't forget to check <a href="http://www.freewebsitetemplates.com">free website templates</a> every day, because we add a new free website template almost daily.</p>
</div>
</div>
<div class="clear"></div>
</div>
</div>
</body>
</html>
Code: Select all
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="rajeshwari"; // Mysql password
$db_name="test"; // Database name
$tbl_name="userdetails"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$username=$_POST['username'];
$password=$_POST['password'];
$email=$_POST['email'];
$location=$_POST['location'];
echo "$firstname";
$query = mysql_query("SELECT * FROM userdetails WHERE username='$username'");
$numrows = mysql_num_rows($query);
if ($numrows!=0)
{
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
if ($username==$dbusername)
{
echo "This username has been already taken";
}
}
else
{
// Insert data into mysql
$sql="INSERT INTO $tbl_name(id, firstname, lastname, username, password, email, location)VALUES(NULL, '$firstname', '$lastname','$username','$password','$email','$location')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result)
{
echo "Your information has been Successful added in the Database!!!";
echo "<BR>";
echo "<a href='index.php'>Back to main page</a>";
echo "<a href='logout.php'>Logout Account</a>";
}
}
}
else {
echo "Error";
}
// close connection
mysql_close();
?>