Page 1 of 5 123 ... LastLast
Results 1 to 10 of 42

Thread: Php login/registration error

  1. #1
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default Php login/registration error

    Hi everyone,

    On my site it's coming up with this error

    Parse error: syntax error, unexpected $end in /home1/keyboard/public_html/Canberra Amatuer Productions/databaseedit.php on line 101

    I've commented it out so I know that the error is somewhere in here

    PHP Code:
    <?php 

     mysql_connect
    ("localhost""****""****") or die(mysql_error()); 
     
    mysql_select_db("****") or die(mysql_error()); 


     if (isset(
    $_POST['submit'])) { 


     if (!
    $_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] | !$_POST['age'] ) {
             die(
    'You did not complete all of the required fields');
         }

    Any help would be appreciated

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Unexpected $end means that your code does not have matching {} pairs. (Or something similar).

    In this case it looks like your middle if statement is not ended. Add } to the end and it should be fine. Unless you already have that ended somewhere else.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    also, | is probably not the operator you're intending to use. It may be working for your specific case at the moment, but it won't be reliable.

    | is the bitwise "OR" operator. It compares the state of the bits (i.e., the 1's and 0's) in the binary values (not the string/boolean/integer/etc. values) and returns the bit values that all of the given values include. Here's a good introductory tutorial on the subject.

    for comparing values the way it appears you intend to, you need to be using || - the logical "OR" operator. it compares the evaluated result of each term (i.e., "true" or "false").

    also also, ! $somevar is not the best way to do it. If $somevar is not set, then this will still evaluate the way you expect, but it will also throw a warning. I generally use empty($somevar) , which does the same evaluation but also checks if the variable is set or not (empty($somevar) is like !isset($somevar) || $somevar == FALSE).
    Last edited by traq; 07-27-2011 at 02:58 AM.

  4. The Following User Says Thank You to traq For This Useful Post:

    djr33 (07-27-2011)

  5. #4
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    The idea is that the form submits to the page the form is on (self) and once it has been submited it will then run the checks to see if the fields are empty etc.

    Is this what the script should look like
    PHP Code:
     if (isset($_POST['submit'])) { 


     if (!
    $_POST['username'] || !$_POST['pass'] || !$_POST['pass2'] || !$_POST['age'] ) }
    {
             die(
    'You did not complete all of the required fields');
         } 

  6. #5
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    No, you're closing the first conditional with the second conditional in it.

    PHP Code:
     if (isset($_POST['submit'])) { 


     if (!
    $_POST['username'] || !$_POST['pass'] || !$_POST['pass2'] || !$_POST['age'] ){
             die(
    'You did not complete all of the required fields');
         }  
    //Whatever else is done when the form is submitted
    } else {
    //what ever is done when the form isnt submitted
    // remove from 'else {' if you dont want to distinguish 
    Also as Traq says, it's better to use the empty so you don't fill your error log with warnings.

    empty($somevar);
    Traq, do you know why it throws the warning, you are stilling using the value in the empty, so it is still undefined. For example I've had to use this

    Code:
    if (!empty($_SERVER['HTTP_REFERER']))
    		$refer = $_SERVER['HTTP_REFERER'];
    	else 
    		$refer = '';
    		<input type="hidden" name="refer"  value="<?php echo $refer; ?>" />
    Instead of just

    Code:
    <input type="hidden" name="refer"  value="<?php echo $_SERVER['HTTP_REFERER']; ?>" />
    Seems like extra work, outputting nothing there won't/doesn't break anything. Thanks.
    Last edited by bluewalrus; 07-27-2011 at 04:59 AM. Reason: Added question to traq and add OP should use empty
    Corrections to my coding/thoughts welcome.

  7. #6
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    PHP Code:
    <?php 
     
    // Connects to your Database 
     
    mysql_connect("localhost""****""****") or die(mysql_error()); 
     
    mysql_select_db("****") or die(mysql_error()); 

     
    //This code runs if the form has been submitted
     
    if (isset($_POST['submit'])) { 

     
    //This makes sure they did not leave any fields blank
     
    if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] ) {
             die(
    'You did not complete all of the required fields');
         }

     
    // checks if the username is in use
         
    if (!get_magic_quotes_gpc()) {
             
    $_POST['username'] = addslashes($_POST['username']);
         }
     
    $usercheck $_POST['username'];
     
    $check mysql_query("SELECT username FROM users WHERE username = '$usercheck'"
    or die(
    mysql_error());
     
    $check2 mysql_num_rows($check);

     
    //if the name exists it gives an error
     
    if ($check2 != 0) {
             die(
    'Sorry, the username '.$_POST['username'].' is already in use.');
                     }

     
    // this makes sure both passwords entered match
         
    if ($_POST['pass'] != $_POST['pass2']) {
             die(
    'Your passwords did not match. ');
         }

         
    // here we encrypt the password and add slashes if needed
         
    $_POST['pass'] = md5($_POST['pass']);
         if (!
    get_magic_quotes_gpc()) {
             
    $_POST['pass'] = addslashes($_POST['pass']);
             
    $_POST['username'] = addslashes($_POST['username']);
                 }

     
    // now we insert it into the database
         
    $insert "INSERT INTO users (username, password)
                 VALUES ('"
    .$_POST['username']."', '".$_POST['pass']."')";
         
    $add_member mysql_query($insert);
         
    ?>

     
     <h1>Registered</h1>
     <p>Thank you, you have registered - you may now login</a>.</p>

    <?php 
     

     else 
     {    
     
    ?>

     <?php include("links.php"); ?>
     <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
     <table border="0">
     <tr><td colspan=2><h1>Register</h1></td></tr> 
     <tr><td>Username:</td><td>
     <input type="text" name="username" maxlength="60">
     </td></tr>
     <tr><td>Password:</td><td>
     <input type="password" name="pass" maxlength="10">
     </td></tr>
     <tr><td>Confirm Password:</td><td>
     <input type="password" name="pass2" maxlength="10">
     </td></tr>
     <tr><th colspan=2><input type="submit" name="submit" value="Register"></th></tr> </table>
     </form>

     <?php
     
    }
     
    ?>
    <html>

    <head>
    <body style="background-color:lightgreen">
    </head>
    <body>

    </body>

    </html>


    This is the origanal code and it works fine. I'm trying to add in more fields like age, gender, email, actor/director, first name, Last name. I would like to have age and actor/director as radio inputs.

    How do I include all this into the script and also how are radio inputs stored in an sql database

  8. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by bluewalrus View Post
    Traq, do you know why it throws the warning, you are stilling using the value in the empty, so it is still undefined. For example I've had to use this

    Code:
    if (!empty($_SERVER['HTTP_REFERER']))
    		$refer = $_SERVER['HTTP_REFERER'];
    	else 
    		$refer = '';
    		<input type="hidden" name="refer"  value="<?php echo $refer; ?>" />
    Instead of just

    Code:
    <input type="hidden" name="refer"  value="<?php echo $_SERVER['HTTP_REFERER']; ?>" />
    yeah, I do similar stuff all the time. I typically use the ternary operator, though (I love that thing)
    PHP Code:
    $val = empty($VAL) ? NULL$VAL
    <sorry> </thread-hijack> </sorry>

  9. #8
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Quote Originally Posted by traq View Post
    <sorry> </thread-hijack> </sorry>
    Thats ok

  10. #9
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Anyone?

  11. #10
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Well, I don't know you would make the age radio buttons and have it fit practically, so I modified it so that it would populate a select combo box with ages 1 - 110 (I also modified it to have the fields: firstname, lastname, age, gender, email, actor)

    PHP Code:
    <?php 
     
    // Connects to your Database 
     
    mysql_connect("localhost""****""****") or die(mysql_error()); 
     
    mysql_select_db("****") or die(mysql_error()); 

     
    //This code runs if the form has been submitted
     
    if (isset($_POST['submit'])) { 

     
    //This makes sure they did not leave any fields blank
     
    if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] ) {
             die(
    'You did not complete all of the required fields');
         }

     
    // checks if the username is in use
         
    if (!get_magic_quotes_gpc()) {
             
    $_POST['username'] = addslashes($_POST['username']);
         }
     
    $usercheck $_POST['username'];
     
    $check mysql_query("SELECT username FROM users WHERE username = '$usercheck'"
    or die(
    mysql_error());
     
    $check2 mysql_num_rows($check);

     
    //if the name exists it gives an error
     
    if ($check2 != 0) {
             die(
    'Sorry, the username '.$_POST['username'].' is already in use.');
                     }

     
    // this makes sure both passwords entered match
         
    if ($_POST['pass'] != $_POST['pass2']) {
             die(
    'Your passwords did not match. ');
         }

         
    // here we encrypt the password and add slashes if needed
         
    $_POST['pass'] = md5($_POST['pass']);
         if (!
    get_magic_quotes_gpc()) {
             
    $_POST['pass'] = addslashes($_POST['pass']);
             
    $_POST['username'] = addslashes($_POST['username']);
                 }
                 
         if(
    $_POST[firstname] != '' && $_POST[firstname] != '' && $_POST[firstname] != '' && $_POST[firstname] != '') {
     
    // now we insert it into the database
         
    $insert "INSERT INTO users (username, password, firstname, lastname, age, gender, email, actor)
                 VALUES ('"
    .$_POST['username']."', '".$_POST['pass']."', '$_POST[firstname]', '$_POST[lastname]', '$_POST[age]', '$_POST[gender]', '$_POST[email]', '$_POST[actor]')";
         
    $add_member mysql_query($insert);
         
         echo <<<EOF
         <h1>Registered</h1>
     <p>Thank you, you have registered - you may now login</a>.</p>
    EOF;

         } else {
             echo 
    "Please fill in all of the required fields.";
         }

     } 
     else 
     {    
     
    ?>
    age, gender, email, actor/director, first name, Last name

     <?php include("links.php"); ?>
     <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
     <table border="0">
     <tr><td colspan="2"><h1>Register</h1></td></tr> 
     <tr><td>Username:</td><td>
     <input type="text" name="username" maxlength="60">
     </td></tr>
     <tr><td>Password:</td><td>
     <input type="password" name="pass" maxlength="10">
     </td></tr>
      <tr><td>First name:</td><td>
     <input name="lastname" type="text" maxlength="10">
     </td></tr>
      <tr><td>Last name:</td><td>
     <input name="firstname" type="text" maxlength="10">
     </td></tr>
      <tr><td>Age:</td><td>
      <select name="age">
    <?php $i 1; while($i <= 110) { echo "<option value=\"$i\">$i</option>"$i++; } ?>
    </select>
     </td></tr>
       <tr><td>Email:</td><td>
     <input name="email" type="text" maxlength="10">
     </td></tr>
     <tr><td>Confirm Password:</td><td>
     <input type="password" name="pass2" maxlength="10">
     </td></tr>
      <tr><td>Actor/Director?:</td><td>
     <input type="password" name="actor" value="actor" maxlength="10"> Actor<br>
      <input type="password" name="actor" value="director" maxlength="10"> Director
     </td></tr>
     <tr><th colspan="2"><input type="submit" name="submit" value="Register"></th></tr> </table>
     </form>

     <?php
     
    }
     
    ?>
    <html>

    <head>
    <body style="background-color:lightgreen">
    </head>
    <body>

    </body>

    </html>
    Keep in mind that you would need to modify your MySQL database to include these extra fields:
    firstname, lastname, age, gender, email, actor

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •