November 23rd, 2012, 09:43 PM
-
New to php and i have a register problem
Hey guys, im getting an error in where "?>" is, i just added the check if email and user has already been taken part and i just started getting this error.
Code:
<?php include "includes/base.php"; ?>
<!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" />
<!-- TemplateBeginEditable name="doctitle" -->
<title>Register</title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
<!-- TemplateEndEditable -->
<!-- TemplateBeginEditable name="head" -->
<!-- TemplateEndEditable -->
</head>
<body>
<div id="main">
<?php
if(!empty($_POST['username']) && !empty($_POST['password']))
{
$username = mysql_real_escape_string($_POST['username']);
$password = (mysql_real_escape_string($_POST['password']));
$email = mysql_real_escape_string($_POST['email']);
$first = mysql_real_escape_string($_POST['first']);
$middle = mysql_real_escape_string($_POST['middle']);
$last = mysql_real_escape_string($_POST['last']);
$Security_Question = mysql_real_escape_string($_POST['Security_Question']);
$answer = mysql_real_escape_string($_POST['answer']);
$checkexists = mysql_query("SELECT username, email FROM register WHERE Username = '".$username."' OR Email= '".$email."'");
if(mysql_num_rows($checkexists) ==1)
{
while ($rst = mysql_fetch_array($check_exists, MYSQL)) {
if ( $rst["username"] == $_POST['username'] )
{
$error_message .="Sorry, that username is taken. Please go back and try again.";
}
elseif ( $rst["email"] == $_POST['email'] )
{
$error_message .="Your email is already taken, if this is not you please contact us to resolve this issue. ";
}
else
{
$registerquery = mysql_query("INSERT INTO users (Username, Password, Email, First, Middle, Last, Security_Question, Answer) VALUES('".$username."', '".$password."', '".$email."', '".$first."',
'".$middle."', '".$last."', '".$Security_Question."', '".$answer."')");
if($registerquery)
{
echo "<h1>Success</h1>";
echo "<p>Your account was successfully created. Please <a href=\"index.php\">click here to login</a>.</p>";
}
else
{
echo "<h1>Error</h1>";
echo "<p>Sorry, your registration failed. Please go back and try again.</p>";
}
}
}
{
?>
<h1>Register</h1>
<p>Please enter your details below to register.</p>
<form method="post" action="register.php" name="registerform" id="registerform">
<fieldset>
<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
<label for="email">Email:</label><input type="text" name="email" id="email" /><br />
<label for="first">First Name:</label><input type="text" name="first" id="first" /><br />
<label for="middle">Middle Name:</label><input type="text" name="middle" id="middle" /><br />
<label for="last">Last Name:</label><input type="text" name="last" id="last" /><br />
<label for="question">Question:</label><input type="text" name="Security_Question" id="Security_Question" /><br />
<label for="answer">Answer:</label><input type="text" name="answer" id="answer" /><br />
<input type="submit" name="register" id="register" value="Register" />
</fieldset>
</form>
<?php
}
?>
</div>
</body>
</html>
if you have any tips or are able to help its much appreciated
Thanks
November 24th, 2012, 12:11 AM
-
Hi,
which error do you get at which line? Do you want us to guess?
In any case, the braces at your "while" loop in line 31 are messed up. Where's the closing brace of the loop? Where's the closing brace of the "elseif" in line 40?
November 24th, 2012, 12:37 AM
-
Hi, sorry about that, the error is at line: 90,
Its telling me all of the following is errors actually
Code:
?>
</div>
</body>
</html>
So 87-90.
November 24th, 2012, 12:48 AM
-
And what about the error message and the braces?
November 24th, 2012, 01:13 AM
-
Dreamweaver doesn't define any errors in line 31 or 40
Sorry, i'm pretty new at this
November 24th, 2012, 01:25 AM
-
Well, have you looked at the statements I was talking about?
The PHP parser cannot really help you with missing braces, because only you as the programmer know where the statements are supposed to end. All the parser can do is tell you at the very last "?>" that you haven't closed all opening braces.
Errors can't always be located exactly. If that was possible, the program might as well correct itself. So you still have to look for yourself and see what's wrong.
November 24th, 2012, 09:24 AM
-
Ya, make sure all your open tags get closed. Are there any included files with this setup? If you include a 50 line file in a 100 line file, your error may even reference on account of 150 lines.
EDIT: Wow. I suppose I should actually look at everything before I ask a question. ^_^ I see there's no include(), but just keep that as a note for future refence.
N compliments on your layout/indentation. If you want to save a little room on your line count, you can relocate your curly brackets, and leave just the final closing one on its own.
if() {
} else {
}
Last edited by Triple_Nothing; November 24th, 2012 at 09:30 AM.
November 24th, 2012, 09:45 AM
-
Ya ya. Must be a morning thing. I now see the include()... Bleh.
Well, I re-wrote your code a lil bit below, adding in needed closing curly brackets. My question still remains about the final { } you had. It was just wrapping the HTML. Is there reasoning for this?
PHP Code:
<?php include "includes/base.php"; ?>
<!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" />
<!-- TemplateBeginEditable name="doctitle" -->
<title>Register</title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
<!-- TemplateEndEditable -->
<!-- TemplateBeginEditable name="head" -->
<!-- TemplateEndEditable -->
</head>
<body>
<div id="main">
<?php
if(!empty($_POST['username']) && !empty($_POST['password'])) {
$username = mysql_real_escape_string($_POST['username']);
$password = (mysql_real_escape_string($_POST['password']));
$email = mysql_real_escape_string($_POST['email']);
$first = mysql_real_escape_string($_POST['first']);
$middle = mysql_real_escape_string($_POST['middle']);
$last = mysql_real_escape_string($_POST['last']);
$Security_Question = mysql_real_escape_string($_POST['Security_Question']);
$answer = mysql_real_escape_string($_POST['answer']);
$checkexists = mysql_query("SELECT username, email FROM register WHERE Username = '".$username."' OR Email= '".$email."'");
if(mysql_num_rows($checkexists) ==1) {
while ($rst = mysql_fetch_array($check_exists, MYSQL)) {
if ( $rst["username"] == $_POST['username'] ) {
$error_message .="Sorry, that username is taken. Please go back and try again.";
} elseif ( $rst["email"] == $_POST['email'] ) {
$error_message .="Your email is already taken, if this is not you please contact us to resolve this issue. ";
} else {
$registerquery = mysql_query("INSERT INTO users (Username, Password, Email, First, Middle, Last, Security_Question, Answer) VALUES('".$username."', '".$password."', '".$email."', '".$first."', '".$middle."', '".$last."', '".$Security_Question."', '".$answer."')");
if($registerquery) {
echo "<h1>Success</h1>";
echo "<p>Your account was successfully created. Please <a href=\"index.php\">click here to login</a>.</p>";
} else {
echo "<h1>Error</h1>";
echo "<p>Sorry, your registration failed. Please go back and try again.</p>";
}
}
}
}
}
?>
<h1>Register</h1>
<p>Please enter your details below to register.</p>
<form method="post" action="register.php" name="registerform" id="registerform">
<fieldset>
<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
<label for="email">Email:</label><input type="text" name="email" id="email" /><br />
<label for="first">First Name:</label><input type="text" name="first" id="first" /><br />
<label for="middle">Middle Name:</label><input type="text" name="middle" id="middle" /><br />
<label for="last">Last Name:</label><input type="text" name="last" id="last" /><br />
<label for="question">Question:</label><input type="text" name="Security_Question" id="Security_Question" /><br />
<label for="answer">Answer:</label><input type="text" name="answer" id="answer" /><br />
<input type="submit" name="register" id="register" value="Register" />
</fieldset>
</form>
</div>
</body>
</html>
November 24th, 2012, 10:17 AM
-
Triple_Nothing, don't just dump your ready-made code for the OP to copy and paste, that rarely helps.
November 24th, 2012, 10:21 AM
-
Sorry. The only true edits were the missing brackets he was already informed of, n I removed the ones I still questioned about. It's intent was more a visual goal with line seperation n all than coding details.