PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 25th, 2005, 10:50 AM
SnapCracker's Avatar
SnapCracker SnapCracker is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Location: Kent, United Kingdom
Posts: 165 SnapCracker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 21 h 22 m 31 sec
Reputation Power: 4
Nested if statements causing parse error

Hi I am trying to use phpfreaks "Creating a Membership System" which should be familiar to us as the tutorial is posted somewhere in DevShed however, with a few missing links to example of db.php and the complete download at the end. Saying that it is complete on the phpfreaks.com and that's where I'm refering to. My question is, in the register.php there is a block of code which checks for blanks in the input form by having what look like quite a logical group of if statements;
PHP Code:
if((!$first_name) || (!$last_name) || (!$email_address) || (!$username)){ 
****echo 
'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 
'join_form.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! 



It comes up with: parse error, unexpected T_ECHO on the line where it reads:- echo 'You did not submit the following required information! <br />';

I know it's someting to do with nesting an 'if' within the bunch of 'if's but why did millions of people have no trouble and I do?
I have a feeling I might have to use a 'while' statement, but how I don't know. Is it my version of php on my hosting site(1&1) which is 4.3.8?

Reply With Quote
  #2  
Old February 25th, 2005, 12:21 PM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 5
What are all those asterisks?

Reply With Quote
  #3  
Old February 25th, 2005, 12:34 PM
SnapCracker's Avatar
SnapCracker SnapCracker is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Location: Kent, United Kingdom
Posts: 165 SnapCracker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 21 h 22 m 31 sec
Reputation Power: 4
The've been put in by the php tags! this is the first time I have used the tags for code, because sometimes you see empty spaces where the code should be and it's quite frustrating to see:-

..........and the your code should be!

PHP Code:
  

However, if you click on the quick reply link the code comes up! Anyway, here is the code

if((!$first_name) || (!$last_name) || (!$email_address) || (!$username)){
echo '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 'join_form.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!
}

hope that come out alright!

Reply With Quote
  #4  
Old February 25th, 2005, 01:43 PM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 5
Post a few lines of the code just before your if statements; there may be something there.

Reply With Quote
  #5  
Old February 25th, 2005, 02:03 PM
SnapCracker's Avatar
SnapCracker SnapCracker is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Location: Kent, United Kingdom
Posts: 165 SnapCracker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 21 h 22 m 31 sec
Reputation Power: 4
Hi
just before the checking process is:

/* 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);

I know that it all works if you have just the first if statement

if((!$first_name) || (!$last_name) || (!$email_address) || (!$username)) {
echo ("You did not submit the following required information! <br />");
}
but when you include the other if statements in between you get the error.

Reply With Quote
  #6  
Old February 25th, 2005, 04:58 PM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 5
I'm not seeing anything that should be giving you an echo error, but I did notice you posted the if code slightly differently in your later post. Please post the code exactly as you have it -- give me about ten lines on either side of the if routine. Go ahead and put it in PHP tags, even if it does add the asterisks, and I'll take another crack at it. There's some sort of syntax thing going on -- it's just hitting the right line.

Reply With Quote
  #7  
Old February 25th, 2005, 06:05 PM
SnapCracker's Avatar
SnapCracker SnapCracker is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Location: Kent, United Kingdom
Posts: 165 SnapCracker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 21 h 22 m 31 sec
Reputation Power: 4
The tutorial is on this link:- http://www.phpfreaks.com/tutorials/40/3.php
This is the surrounding code:-
PHP Code:
// 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)){
    echo 
'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 
'join_form.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'");
 
 
$email_check mysql_num_rows($sql_email_check);
 
$username_check mysql_num_rows($sql_username_check);
 
 if((
$email_check 0) || ($username_check 0)){
     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 
'join_form.html'// Show the form again!
     
exit();  // exit the script so that we do not create this account!
 



So the syntax is correct for the 'if' statements? because this works and does not produce any error
PHP Code:
// 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);
 
/* I print the variables to check whether they are parsed */
print "$first_name<br>";
print 
"$last_name<br>";
print 
"$email_address<br>";
print 
"$username";

if((!
$first_name) || (!$last_name) || (!$email_address) || (!$username)){

echo (
"You did not submit the following required information! <br />");




Reply With Quote
  #8  
Old February 26th, 2005, 11:40 AM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 5
Whatever it is, I'm not seeing it. I don't have PHP handily available right now, either -- I'll run some tests on it when I can.

Reply With Quote
  #9  
Old February 28th, 2005, 09:24 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
I suggest putting or die() on the end of your mysql_query() functions.
Maybe you're database is returning an error.

Reply With Quote
  #10  
Old February 28th, 2005, 11:39 AM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 5
I tried your script locally, and it's working fine. What data are you putting into the form for these tests?

Reply With Quote
  #11  
Old February 28th, 2005, 05:11 PM
SnapCracker's Avatar
SnapCracker SnapCracker is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Location: Kent, United Kingdom
Posts: 165 SnapCracker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 21 h 22 m 31 sec
Reputation Power: 4
Hi Madpawn!
I think I've found the major contribution to the problem. If you recall, you asked about those asterisks, well they must have been picked up when I cut and pasted from the webpage. In Dreamweaver, they show as spaces so they went undetected. Deleting any spaces on the left hand side mopped up most of those unexpected T-Variable errors. However, there are a few things that it falls over for. One is :
PHP Code:
or die (mysql_error()); 

change this to :
PHP Code:
or die (); 

it's happy!

Also, I get parse error, unexpected '}' in the last line for this bit.
PHP Code:
/* 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'"); 
$email_check mysql_num_rows($sql_email_check); 
$username_check mysql_num_rows($sql_username_check); 
if((
$email_check 0) || ($username_check 0))
{
    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 
'/phplogin/join_form.html'// Show the form again! 
        
exit();† // exit the script so that we do not create this account! 



And lastly it fall over because of :function makeRandomPassword() {
PHP Code:
function makeRandomPassword() {
†$salt "abchefghjkmnpqrstuvwxyz0123456789";
†$pass "";
†srand((double)microtime()*1000000);
†    $i 0;
†    while ($i <= 7) {
† † †$num rand() % 33;
† † †$tmp substr($salt$num1);
† † †$pass $pass $tmp;
† † †$i++;
†    }
†    return $pass;
}
$random_password makeRandomPassword();
$db_password md5($random_password); 

Can you see those †'s, that's what making it fall over and I can only view them if paste the offending code into this message box and then delete them in Dreamweaver.
I'm really sorry guys, I think you have been stratching your heads abit with this one! I really didn't expect this.

Reply With Quote
  #12  
Old February 28th, 2005, 05:54 PM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 5
That's pretty weird. I'm far from a dreamweaver expert (I basically just use it as a plain editor, but I've never run into that. There must be something you have set, perhaps having to do with indention or tabs, that's causing that. I did notice you have one of those t' after your exit() call -- that's probably what's causing your other parse error.

As for the mysql_error() causing trouble -- that's got me totally stumped. There's no reason that shouldn't work. What error does it give you, exactly?

Reply With Quote