SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Feb 2007
    Location
    UK
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy I know there's a syntax error ... I just can't see it!

    I've just run this PHP script and it keeps coming back with the following syntax error:

    Parse error: syntax error, unexpected T_ELSE in /home/.yankee/cynical/obliviat.com/admin/addmember.php on line 52

    I've checked through the code several times, using different techniques to spot the error and I just can't seem to find it. I reckon that it should be around the $mysql variable, according to the error message.

    Would someone who hasn't been looking at this code for the past half an hour be kind enough to try and spot what I'm missing please?

    PHP Code:
    <?php 

    include 'db.inc.php';

    //checks cookies to make sure they are logged in 
    if(isset($_COOKIE['ID_my_site'])) 

    $username $_COOKIE['ID_my_site']; 
    $pass $_COOKIE['Key_my_site']; 
    $check mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); 
    while(
    $info mysql_fetch_array($check)) 


        
    //if the cookie has the wrong password, they are taken to the login page 
        
    if ($pass != $info['password']) 
        { 
    header("Location: index.php"); 
        } 
        else {
        
        
    $name $_POST['name'];
        
    $oocname $_POST['oocname'];
        
    $position $_POST['position'];
        
    $skills $_POST['skills'];
        
    $bio $_POST['bio'];
        
    $ordoid $_POST['ordoid'];
        
    $sql "INSERT INTO member SET
            name='
    $name',
            oocname='
    $oocname',
            position='
    $position',
            skills='
    $skills',
            bio='
    $bio',
            ordoid='
    $ordoid'";
        if (@
    mysql_query($sql)) {
            echo 
    '<p> New member added</p>';
        }    else {
            echo 
    '<p>Error adding new member: ' .
                
    mysql_error() . '</p>';
        }
    ?>

    <p><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Add another member</a></p>
    <p><a href="memberlist.php">Return to member list</a></p>

    <?php else: // Allow user to add new member ?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <p>Enter new member details:<br /><br />
    <label>Name: <input type="text" name="name" /></label><br />
    <label>OOC Name: <input type="text" name="oocname" /></label><br />
    <p>Select Order:
        <select name="ordoid" size="1">
          <option selected value="">Select One</option>
          <option value="">---------</option>
        <?php
          
    while ($order mysql_fetch_array($orders)) {
            
    $ordoid $order['id'];
            
    $oname htmlspecialchars($order['ordername']);
            echo 
    "<option value='$ordoid'>$oname</option>\n";
          }
        
    ?>
        </select></p>
    <label>Position in Temple: <input type="text" name="position" /></label><br />
    <label>Skills: <input type="text" name="skills" /></label><br />
    <label>Biography:<textarea name="bio" rows="5" cols="45"></textarea><br /></label>
    <input type="submit" value="SUBMIT" /></p>
    </form>

    <?php endif; 

        }
    else 
    //if the cookie does not exist, they are taken to the login screen 
    {    
    header("Location: index.php"); 

    }
    }

    ?>
    Meep.
    Cynical Matrix - dumping ground for a self-confessed geek

  2. #2
    SitePoint Addict
    Join Date
    May 2005
    Location
    London, ON
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are forgetting to close a bunch of loops. I've properly indented the code and closed loops at the end.

    I'm not sure if the end is where you want them closed or not, I'll leave that up to you - just remember to close if/else and while statements.

    PHP Code:
    <?php

    include 'db.inc.php';

    //checks cookies to make sure they are logged in
    if(isset($_COOKIE['ID_my_site'])) {
        
    $username $_COOKIE['ID_my_site'];
        
    $pass $_COOKIE['Key_my_site'];
        
    $check mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
        while(
    $info mysql_fetch_array($check)) {

            
    //if the cookie has the wrong password, they are taken to the login page
            
    if ($pass != $info['password']) { 
                
    header("Location: index.php"); 
            } else {
        
                
    $name $_POST['name'];
                
    $oocname $_POST['oocname'];
                
    $position $_POST['position'];
                
    $skills $_POST['skills'];
                
    $bio $_POST['bio'];
                
    $ordoid $_POST['ordoid'];
                
    $sql "INSERT INTO member SET
                    name='
    $name',
                    oocname='
    $oocname',
                    position='
    $position',
                    skills='
    $skills',
                    bio='
    $bio',
                    ordoid='
    $ordoid'";
                if (@
    mysql_query($sql)) {
                    echo 
    '<p> New member added</p>';
                } else {
                    echo 
    '<p>Error adding new member: ' .
                        
    mysql_error() . '</p>';
                }
            }
        }
    }
    ?>
    Ryan Price
    Subscribe to my blog for regular tips and tricks

  3. #3
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For one, you have two ELSE on the first IF statement..

    It looks to me like you're trying to do this:
    PHP Code:
    if (isset($_COOKIE)) {
        if (isset(
    $_POST) {
            
    // insert into database
        
    } else {
            
    // output form
        
    }
    } else {
        
    // redirect to index.php
    }
    ?> 
    But that's not how your script is set up..
    - the lid is off the maple syrup again!

  4. #4
    SitePoint Member
    Join Date
    Feb 2007
    Location
    UK
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's still not working. Ho hum. Going to go over the code again with a fine tooth comb and see what's wrong. I'm a bit more awake and might be able to spot the problem a bit easier.

    Thanks for your helps, peeps!
    Meep.
    Cynical Matrix - dumping ground for a self-confessed geek

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Morning, I think you might need to rethink your logic on this one acid_lee!
    Taking it from the top:

    Cookie Check
    PHP Code:
    //checks cookies to make sure they are logged in 
    if(isset($_COOKIE['ID_my_site'])) { 
        
    $username $_COOKIE['ID_my_site']; 
        
    $pass $_COOKIE['Key_my_site']; 
        
    $check mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); 
        
    $info mysql_fetch_assoc($check);
        
    //if the cookie has the wrong password, they are taken to the login page 
        
    if ($pass != $info['password'])  {
            
    header("Location: index.php");
            exit(); 
    // exit the script
        
    }
    // if no cookie....
    } else {
        
    header("Location: index.php");
        exit(); 
    // exit the script    


    // end cookie check 
    Check for the cookie and define your user variables. Compare them and if they are right - do nothing, if they are wrong- redirect. If no cookie - redirect.


    Check for form submission
    PHP Code:
    // check for the post variables
    // use a hidden field to check for the form submit
    // not everyone clicks the submit button!
    if(isset($_POST['form_submit'])) {
          
        $name = $_POST['name']; 
        $oocname = $_POST['oocname']; 
        $position = $_POST['position']; 
        $skills = $_POST['skills']; 
        $bio = $_POST['bio']; 
        $ordoid = $_POST['ordoid']; 
        $sql = "INSERT INTO member SET 
            name='". mysql_real_escape_string($name) ."', 
            oocname='". mysql_real_escape_string($oocname) ."', 
            position='". mysql_real_escape_string($position) ."', 
            skills='". mysql_real_escape_string($skills) ."', 
            bio='". mysql_real_escape_string($bio) ."', 
            ordoid='". mysql_real_escape_string($ordoid) ."'"; 
        if (@mysql_query($sql)) { 
            echo '<p> New member added</p>'; 
        }    else { 
            echo '<p>Error adding new member: ' . 
                mysql_error() . '</p>'; 
        } 

    ?>

    <p><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Add another member</a></p> 
    <p><a href="memberlist.php">Return to member list</a></p> 

    <?php 
    // end form submit 
    ?>
    Check against a hidden field for form submission as some browser will not send the submit button value if the user hits ENTER.

    Remember to sanitise ALL user inputted data using mysql_real_escape_string() or something similar.

    Form
    I am guessing there is another query on the page somewhere for the select menu options....
    PHP Code:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
    <p>Enter new member details:<br /><br /> 
    <label>Name: </label><input type="text" name="name" /><br /> 
    <label>OOC Name: </label><input type="text" name="oocname" /><br /> 
    <p>Select Order: 
        <select name="ordoid" size="1"> 
          <option selected value="">Select One</option> 
          <option value="">---------</option> 
        <?php 
        
    while ($order mysql_fetch_array($orders)) { 
            echo 
    '<option value="'$order['id'] .'">'htmlspecialchars($order['ordername']).'</option>'."\n"
        } 
        
    ?> 
        </select></p> 
    <label>Position in Temple: </label><input type="text" name="position" /><br /> 
    <label>Skills: </label><input type="text" name="skills" /><br /> 
    <label>Biography:</label><textarea name="bio" rows="5" cols="45"></textarea><br /> 
    <input type="hidden" name="form_submit" value="1" />
    <input type="submit" value="SUBMIT" /></p> 
    </form>
    Syntax for elements with labels:
    Code:
    <label>Element Label</label>
    <input type="text" name="myText" id="myText" value="" />
    You had the input inside the <label> tag!

    also
    PHP Code:
    <?php else: // Allow user to add new member ?>
    will throw an error as the comments will remove the closing ?> because they are on the same line.

    Full Script
    PHP Code:
    <?php
    include 'db.inc.php'

    //checks cookies to make sure they are logged in 
    if(isset($_COOKIE['ID_my_site'])) { 
        
    $username $_COOKIE['ID_my_site']; 
        
    $pass $_COOKIE['Key_my_site']; 
        
    $check mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); 
        
    $info mysql_fetch_assoc($check);
        
    //if the cookie has the wrong password, they are taken to the login page 
        
    if ($pass != $info['password'])  {
            
    header("Location: index.php");
            exit(); 
    // exit the script
        
    }
    // if no cookie....
    } else {
        
    header("Location: index.php");
        exit(); 
    // exit the script    


    // end cookie check


    // check for the post variables
    // use a hidden field to check for the form submit
    // not everyone clicks the submit button!
    if(isset($_POST['form_submit'])) {
          
        
    $name $_POST['name']; 
        
    $oocname $_POST['oocname']; 
        
    $position $_POST['position']; 
        
    $skills $_POST['skills']; 
        
    $bio $_POST['bio']; 
        
    $ordoid $_POST['ordoid']; 
        
    $sql "INSERT INTO member SET 
            name='"
    mysql_real_escape_string($name) ."', 
            oocname='"
    mysql_real_escape_string($oocname) ."', 
            position='"
    mysql_real_escape_string($position) ."', 
            skills='"
    mysql_real_escape_string($skills) ."', 
            bio='"
    mysql_real_escape_string($bio) ."', 
            ordoid='"
    mysql_real_escape_string($ordoid) ."'"
        if (@
    mysql_query($sql)) { 
            echo 
    '<p> New member added</p>'
        }    else { 
            echo 
    '<p>Error adding new member: ' 
                
    mysql_error() . '</p>'
        } 

    ?>

    <p><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Add another member</a></p> 
    <p><a href="memberlist.php">Return to member list</a></p> 

    <?php 
    // end form submit 
    ?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
    <p>Enter new member details:<br /><br /> 
    <label>Name: </label><input type="text" name="name" /><br /> 
    <label>OOC Name: </label><input type="text" name="oocname" /><br /> 
    <p>Select Order: 
        <select name="ordoid" size="1"> 
          <option selected value="">Select One</option> 
          <option value="">---------</option> 
        <?php 
        
    while ($order mysql_fetch_array($orders)) { 
            echo 
    '<option value="'$order['id'] .'">'htmlspecialchars($order['ordername']).'</option>'."\n"
        } 
        
    ?> 
        </select></p> 
    <label>Position in Temple: </label><input type="text" name="position" /><br /> 
    <label>Skills: </label><input type="text" name="skills" /><br /> 
    <label>Biography:</label><textarea name="bio" rows="5" cols="45"></textarea><br /> 
    <input type="hidden" name="form_submit" value="1" />
    <input type="submit" value="SUBMIT" /></p> 
    </form>
    Hope this helps
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    SitePoint Member
    Join Date
    Feb 2007
    Location
    UK
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've got it now. Thanks!
    Meep.
    Cynical Matrix - dumping ground for a self-confessed geek


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
  •