Hey everyone,

I am fairly new to the programming world, but I am a quick learner. I am trying to make my "membership area" register.php work properly, but there seems to be a parsing error as follows here: "Parse error: syntax error, unexpected T_VARIABLE in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\register.php on line 54" ...I understand that this might be a bit annoying to most of the people who probably answer this sort of thing a lot, but I would greatly apreciate the help, since again, I am fairly new to the programming world. The register.php file reads as follows"

<?php

include 'db.php';

// Define post fields into simple variables
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_address = $_POST['email_address'];
$username = $_POST['username'];
$info = $_POST['info'];

/* Let's strip some slashes in case the user entered
any escaped characters. */

$first_name = stripslashes($first_name);
$last_name = stripslashes($last_name);
$email_address = stripslashes($email_address);
$username = stripslashes($username);
$info = stripslashes($info);


/* Do some error checking on the form posted fields */

if((!$first_name) || (!$last_name) || (!$email_address) || (!$username)){
	print "You did not submit the following required information! <br />";

	if(!$first_name){
		echo "First Name is a required field. Please enter it below.<br />";
	}
	if(!$last_name){
		echo "Last Name is a required field. Please enter it below.<br />";
	}
	if(!$email_address){
		echo "Email Address is a required field. Please enter it below.<br />";
	}
	if(!$username){
		echo "Desired Username is a required field. Please enter it below.<br />";
	}
	include 'Register.html'; // Show the form again!
	/* End the error checking and if everything is ok, we'll move on to
	 creating the user account */
	exit(); // if the error checking has failed, we'll exit the script!
}

	
/* Let's do some checking and ensure that the user's email address or username
 does not exist in the database */
 
 $sql_email_check = mysql_query("SELECT email_address FROM users WHERE email_address='$email_address'");
 $sql_username_check = mysql_query("SELECT username FROM users WHERE username='$username'");
 
 
 if (mysql_num_rows($sql_email_check==true)
 $email_check = mysql_num_rows($sql_email_check);
 $username_check = mysql_num_rows($sql_username_check);
 
 if(($email_check ==true) || ($username_check ==true){
 	echo "Please fix the following errors: <br />";
 	if($email_check > 0){
 		echo "<strong>Your email address has already been used by another member in our database. Please submit a different Email address!<br />";
 		unset($email_address);
 	}
 	if($username_check > 0){
 		echo "The username you have selected has already been used by another member in our database. Please choose a different Username!<br />";
 		unset($username);
 	}
 	include 'Register.html'; // Show the form again!
 	exit();  // exit the script so that we do not create this account!
 }
 
/* Everything has passed both error checks that we have done.
It's time to create the account! */

/* Random Password generator. 
http://www.phpfreaks.com/quickcode/Random_Password_Generator/56.php

We'll generate a random password for the
user and encrypt it, email it and then enter it into the db.
*/

function makeRandomPassword() {
  $salt = "abchefghjkmnpqrstuvwxyz0123456789";
  srand((double)microtime()*1000000); 
  	$i = 0;
  	while ($i <= 7) {
    		$num = rand() % 33;
    		$tmp = substr($salt, $num, 1);
    		$pass = $pass . $tmp;
    		$i++;
  	}
  	return $pass;
}

$random_password = makeRandomPassword();

$db_password = md5($random_password);

// Enter info into the Database.
$info2 = htmlspecialchars($info);
$sql = mysql_query("INSERT INTO users (first_name, last_name, email_address, username, password, info, signup_date)
		VALUES('$first_name', '$last_name', '$email_address', '$username', '$db_password', '$info2', now())") or die (mysql_error());

if(!$sql){
	echo 'There has been an error creating your account. Please contact the webmaster.';
} else {
	$userid = mysql_insert_id();
	// Let's mail the user!
	$subject = "Your Membership at Ready Or Not! Tahirih's Website!";
	$message = "Dear $first_name $last_name,
	Thank you for registering at my website, http://www.mydomain.com!
	
	You are two steps away from logging in and interacting wiht my site.
	
	To activate your membership, please click here: http://www.mydomain.com/activate.php?id=$userid&code=$db_password
	
	Once you activate your memebership, you will be able to login with the following information:
	Username: $username
	Password: $random_password
	
	Thanks!
	Tahirih
	
	This is an automated response, please do not reply!";
	
	mail($email_address, $subject, $message, "From: Tahirih Pellegrino<tahirih@tpelle.com>\nX-Mailer: PHP/" . phpversion());
	echo 'Your membership information has been mailed to your email address! Please check your emain and follow the instructions!';
}

?>

When PHP gives you an error, you need to spend some time desk checking the code to see what you missed. That is a part of programming in PHP as much as writing the code. PHP can't always pin it down to the exact line but it is often the preceding line and occasionally it can be many lines before. When it tells you that something is "unexpected", it almost always means that you have a syntax error on, or prior to, the line number that it reports to you. So, if you carefully check the syntax on line 54 and the immediately preceding line, do you see anything missing?

Are you missing an opening { on 53

commented: This person is very helpful and will go out of their way to help someone +1

Actually it looks like your opening and closing brackets don't match in a few places

if (mysql_num_rows($sql_email_check==true)) {
$email_check = mysql_num_rows($sql_email_check);
$username_check = mysql_num_rows($sql_username_check);
}
if(($email_check ==true) || ($username_check ==true)){

try these for starters and always be aware you need a closing bracket somewhere for your opening bracket () [] {} - hopefully this gets you a few lines along

@momo: now when I added the necessary opening and closing brackets listed here:

if (mysql_num_rows($sql_email_check==true)){
 $email_check = mysql_num_rows($sql_email_check);
 $username_check = mysql_num_rows($sql_username_check);
 }
 if(($email_check ==true) || ($username_check ==true)){
 	echo "Please fix the following errors: <br />";
 	if($email_check > 0){
 		echo "<strong>Your email address has already been used by another member in our database. Please submit a different Email address!<br />";
 		unset($email_address);
 	}
 	if($username_check > 0){
 		echo "The username you have selected has already been used by another member in our database. Please choose a different Username!<br />";
 		unset($username);
 	}
 	include 'Register.html'; // Show the form again!
 	exit();  // exit the script so that we do not create this account!
 }

...it shows these errors now: "Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\register.php on line 53

Notice: Undefined variable: email_check in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\register.php on line 57

Notice: Undefined variable: username_check in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\register.php on line 57

Notice: Undefined variable: pass in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\register.php on line 88
No database selected"...

sorry here is the whole thing again with the correct numbering:

<?php

include 'db.php';

// Define post fields into simple variables
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_address = $_POST['email_address'];
$username = $_POST['username'];
$info = $_POST['info'];

/* Let's strip some slashes in case the user entered
any escaped characters. */

$first_name = stripslashes($first_name);
$last_name = stripslashes($last_name);
$email_address = stripslashes($email_address);
$username = stripslashes($username);
$info = stripslashes($info);


/* Do some error checking on the form posted fields */

if((!$first_name) || (!$last_name) || (!$email_address) || (!$username)){
	print "You did not submit the following required information! <br />";

	if(!$first_name){
		echo "First Name is a required field. Please enter it below.<br />";
	}
	if(!$last_name){
		echo "Last Name is a required field. Please enter it below.<br />";
	}
	if(!$email_address){
		echo "Email Address is a required field. Please enter it below.<br />";
	}
	if(!$username){
		echo "Desired Username is a required field. Please enter it below.<br />";
	}
	include 'Register.html'; // Show the form again!
	/* End the error checking and if everything is ok, we'll move on to
	 creating the user account */
	exit(); // if the error checking has failed, we'll exit the script!
}

	
/* Let's do some checking and ensure that the user's email address or username
 does not exist in the database */
 
 $sql_email_check = mysql_query("SELECT email_address FROM users WHERE email_address='$email_address'");
 $sql_username_check = mysql_query("SELECT username FROM users WHERE username='$username'");
 
 
 if (mysql_num_rows($sql_email_check==true)){
 $email_check = mysql_num_rows($sql_email_check);
 $username_check = mysql_num_rows($sql_username_check);
 }
 if(($email_check ==true) || ($username_check ==true)){
 	echo "Please fix the following errors: <br />";
 	if($email_check > 0){
 		echo "<strong>Your email address has already been used by another member in our database. Please submit a different Email address!<br />";
 		unset($email_address);
 	}
 	if($username_check > 0){
 		echo "The username you have selected has already been used by another member in our database. Please choose a different Username!<br />";
 		unset($username);
 	}
 	include 'Register.html'; // Show the form again!
 	exit();  // exit the script so that we do not create this account!
 }
 
/* Everything has passed both error checks that we have done.
It's time to create the account! */

/* Random Password generator. 
http://www.phpfreaks.com/quickcode/Random_Password_Generator/56.php

We'll generate a random password for the
user and encrypt it, email it and then enter it into the db.
*/

function makeRandomPassword() {
  $salt = "abchefghjkmnpqrstuvwxyz0123456789";
  srand((double)microtime()*1000000); 
  	$i = 0;
  	while ($i <= 7) {
    		$num = rand() % 33;
    		$tmp = substr($salt, $num, 1);
    		$pass = $pass . $tmp;
    		$i++;
  	}
  	return $pass;
}

$random_password = makeRandomPassword();

$db_password = md5($random_password);

// Enter info into the Database.
$info2 = htmlspecialchars($info);
$sql = mysql_query("INSERT INTO users (first_name, last_name, email_address, username, password, info, signup_date)
		VALUES('$first_name', '$last_name', '$email_address', '$username', '$db_password', '$info2', now())") or die (mysql_error());

if(!$sql){
	echo 'There has been an error creating your account. Please contact the webmaster.';
} else {
	$userid = mysql_insert_id();
	// Let's mail the user!
	$subject = "Your Membership at Ready Or Not! Tahirih's Website!";
	$message = "Dear $first_name $last_name,
	Thank you for registering at my website, http://www.mydomain.com!
	
	You are two steps away from logging in and interacting wiht my site.
	
	To activate your membership, please click here: http://www.mydomain.com/activate.php?id=$userid&code=$db_password
	
	Once you activate your memebership, you will be able to login with the following information:
	Username: $username
	Password: $random_password
	
	Thanks!
	Tahirih
	
	This is an automated response, please do not reply!";
	
	mail($email_address, $subject, $message, "From: Tahirih Pellegrino<tahirih@tpelle.com>\nX-Mailer: PHP/" . phpversion());
	echo 'Your membership information has been mailed to your email address! Please check your emain and follow the instructions!';
}

?>

it might be necessary to see your db.php

it is also possible that the } on 56 should go at the end of that block of code at line 70 instead

if (mysql_num_rows($sql_email_check==true)){ //if it was true it would assign the next 2 lines, so it appears false and they are therefore undefined (email_check and username_check)
$email_check = mysql_num_rows($sql_email_check);
$username_check = mysql_num_rows($sql_username_check);
}
if(($email_check ==true) || ($username_check ==true)){

...it shows these errors now: "Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\register.php on line 53

In my experience of this error, the cause is usually always a dodgy query returning FALSE or no connection to the DB.

clearly thus - No database selected"...

I removed the "}" on line 56 and put it on line 70 and it now shows errors here:

"Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\register.php on line 53

Notice: Undefined variable: pass in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\register.php on line 88 No database selected"

the database should be connected..I'm using PHPMyAdmin for my database structure..with EasyPHP..it didn't ask me for a password when I opened up the program for the first time, so therefore that slot in the db file is left as such:

<? 
/*  Database Information - Required!!  */
/* -- Configure the Variables Below --*/
$dbhost = '127.0.0.1';
$dbusername = 'member';
$dbpasswd = '';
$database_name = 'member';

/* Database Stuff, do not modify below this line */

$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd") 
	or die ("Couldn't connect to server.");
	
$db = mysql_select_db("$database_name", $connection)
	or die("Couldn't select database.");
?>

What am I doing wrong?

Oops, missed that bit ...

@Smeagel13: I don't understand why the database wouldn't be connected though..I'm pretty sure I set up the "db.php" file correctly since I'm using EasyPHP with PHPMyAdmin. there is no password because the program did not ask me to set up one:

<? 
/*  Database Information - Required!!  */
/* -- Configure the Variables Below --*/
$dbhost = '127.0.0.1';
$dbusername = 'member';
$dbpasswd = '';
$database_name = 'member';

/* Database Stuff, do not modify below this line */

$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd") 
	or die ("Couldn't connect to server.");
	
$db = mysql_select_db("$database_name", $connection)
	or die("Couldn't select database.");
?>

and I "included that specific file within the "register.php" file.. :/

@momo: everything should be connected..

are the files in the same folder?

I don't use EasyPHP or PHPMyAdmin so I can't help you on that.

Thing I would look at:

Check the user table to see if the user does have a password even if you did not choose one - does PHPMyAdmin default to one?

I'd also make sure you close the connection (yes, I know we don't have one at the minute hehe) at the end of the script with using persistent connections, forgive me if I missed that line but I didn't see it.

Check file security on db.php, can you access it? is it in the same directory as the main script?

@momo: yes the files are in the same folder, so linking shouldn't be an issue.

@Smeagel13: No, the user table does not have a password associated with it and no, there is no default password for PHPMyAdmin..what do you mean by "make sure to close the connection?" and everything is in the same directory, no sub-folders.

@Smeagel13: No, the user table does not have a password associated with it and no, there is no default password for PHPMyAdmin..what do you mean by "make sure to close the connection?" and everything is in the same directory, no sub-folders.

You're creating a persistent connection, which will remain open until you explicitly close it ... Unlike mysql_connect which will lose it's connection at the end of the script.

While Smeagel13 is right about closing the connection seen as you are using pconnect, it is not the cause of your problems...

Are you absolutely sure you don't have a typo in your database or table or user etc

DB = member
Username = member
Table = users

i.e. when you were setting them up?

@momo: When I go to my PHPMyAdmin home..that shows all of my databases, it shows a section that reads:"MySQL

Server: 127.0.0.1 via TCP/IP
Server version: 5.5.16-log
Protocol version: 10
User: root@localhost
MySQL charset: UTF-8 Unicode (utf8)"

Am I supposed to put in those values for example for the user part in the db file or keep it as it says? ..since the database name is called "member" and the table within is called "users"

@Smeagel13 I think I want to keep the connection as is. I researched that function, and that seems like what I want.

No they are fine - but it does seem like it is a local setup issue. I haven't used easyPHP, so can't really advise

p.s. - your databases don't have an underscore there do they (I suspect not) _database

@momo: No, there are no "_"'s in any of my database names

at the bottom of db.php, echo out $connection and $db - let's see what's in them...

@smeagel13 do you mean like this?

echo $connection
echo $db

sorry I'm not sure I understand exactly what you mean..

yup use ; after both

echo $connection;
echo $db;

or

echo "Connection Details: ".$connection."<br/>";
echo "Database Details: ".$db."<br/>";


the . position are important, they are not just fullstops here

@momo: Nothing appears on the screen when I open the db file.

If nothing appears on screen then it should be OK (the connect and select) which the echoes will confirm, but if the echoes are duff and the error messages aren't being displayed that's another issue.

Try my second example with the text:

echo "Connection Details: ".$connection."<br/>";
echo "Database Details: ".$db."<br/>";

At the very least the text here will show up - then just open your usual file. As its included - it will run through this lil code echo...

@Smeagel13, not stepping on your toes here, just trying to help too

@Smeagel13, not stepping on your toes here, just trying to help too

don't worry, 2 minds are better than 1 ...

@momo: this is what was displayed: "; echo "Database Details: ".$db."
"; ?>"

Odd, try this again without the text of the previous...

echo $connection;
echo $db;

Anything?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.