Parse error: syntax error, unexpected $end in...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mideastgirl
    New Member
    • Jun 2009
    • 65

    Parse error: syntax error, unexpected $end in...

    I keep getting this error and I cannot figure it out. My curly brackets are closed, and I am using the correct tags for <?php to open and ?> to close my code. Can someone please help me!

    Here is my code:
    [code=php]
    <?php
    //Connect To Database
    $hostname='mide asthonors2.db.4 381361.hostedre source.com';
    $username='**** **********';
    $password='**** *******';
    $dbname='mideas thonors2';
    $usertable='adm in_tasks';
    $yourfield = 'Name';

    mysql_connect($ hostname,$usern ame, $password) OR DIE ('Unable to connect to database! Please try again later.');
    mysql_select_db ($dbname);

    $query = 'SELECT * FROM $usertable';
    $result = mysql_query($qu ery);
    if($result) {
    while($row = mysql_fetch_arr ay($result)){
    $name = $row['$yourfield'];
    echo 'Name: '.$name;

    mysql_select_db ("mideasthonors 2");

    $sql="INSERT INTO $usertable (Name, Address 1, Address 2, City, State, Zip Code, Website, Contact Name, Position, Phone Number, Email Address)

    VALUES

    {$_POST['name']},{$_POST['address 1']},{$_POST['address 2']},{$_POST['city']},{$_POST['state']},{$_POST['zip code']},{$_POST['website']},{$_POST['contact name']},{$_POST['position']},{$_POST['phone number']},{$_POST['email address']};

    if (!mysql_query($ sql,$con))

    {

    die('Error: ' . mysql_error());

    }

    echo '1 record added';

    mysql_close($co n);
    }
    }
    ?>[/code]
    Last edited by Atli; Jun 23 '09, 06:07 PM. Reason: Reason: Added [code] tags and moved this out of the MySQL insights forum into the PHP questions forum.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    You don't close your string properly on line #26.
    That's why you get this error.

    Also, to close a huge security hole in that code.
    Don't put user input directly into SQL queries... or anything, for that matter.
    Always validate it and sanitize it before using it.
    The mysql_real_esca pe_query function is extremely helpful here.

    Check out SQL Injection in the manual. Explains why this is so important.

    Comment

    • mideastgirl
      New Member
      • Jun 2009
      • 65

      #3
      unexpected $end

      How would I end line 26? I have tried removing the semi-colon and using ) instead of } and I still keep getting the same result. I am not really sure what to try next.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Close the string with a double-quote before the semi-colon.

        The string should be enclosed in quote-marks, but you only ever open the string, forgetting to close it with the second quote-mark.

        The problem is basically:
        [code=php]
        // You are doing this:
        $sql = "INSERT INTO whatever;

        // Where you should be doing
        $sql = "INSERT INTO whatever";
        [/code]
        The variables, which you correctly enclose in brackets, are not a part of the problem, and should work fine if left alone as they are.

        Comment

        • mideastgirl
          New Member
          • Jun 2009
          • 65

          #5
          syntax error, unexpected '{' ...on line 26

          Now it is telling me the brackets are wrong on line 26. The issue the whole time has been with line 26, however I did go ahead and make the changes you suggested:
          [code=php]
          mysql_connect($ hostname,$usern ame, $password) OR DIE ('Unable to connect to database! Please try again later.');
          mysql_select_db ($dbname);

          $query = 'SELECT * FROM $usertable';
          $result = mysql_query($qu ery);
          if($result) {
          while($row = mysql_fetch_arr ay($result)){
          $name = $row['$yourfield'];
          echo 'Name: '.$name;

          mysql_select_db ("mideasthonors 2");

          $sql="INSERT INTO $usertable (Name, Address 1, Address 2, City, State, Zip Code, Website, Contact Name, Position, Phone Number, Email Address)";

          VALUES

          {$_POST['name']},{$_POST['address 1']},{$_POST['address 2']},{$_POST['city']},{$_POST['state']},{$_POST['zip code']},{$_POST['website']},{$_POST['contact name']},{$_POST['position']},{$_POST['phone number']},{$_POST['email address']};

          if (!mysql_query($ sql,$con))

          {

          die('Error: ' . mysql_error());

          }

          echo '1 record added';

          mysql_close($co n);
          }
          }
          ?>[/code]
          Last edited by Atli; Jun 24 '09, 01:32 PM. Reason: Added [code] tags.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            You closed the string in the wrong line.

            Everything you want to have inside a string must be enclosed inside the quotes.
            You have like 3 lines there that should be inside your string, but you close the string after the first line.

            You are doing something like this:
            [code=php]
            $str = "This string spans";
            multiple lines and
            should not be closed
            until after the last line;
            [/code]
            So PHP, having closed the string after the first line, tries to parse the rest of what should be inside the string as PHP code, obviously failing.

            It should look like:
            [code=php]
            $str = "This string spans
            multiple lines and
            should not be closed
            until after the last line";
            [/code]

            Comment

            • mideastgirl
              New Member
              • Jun 2009
              • 65

              #7
              syntax error, unexpected '}' in ...on line 23

              So I put the quote at the end of line 26, which solved that problem. Now at the bottom of the script where the last 2 curly brackets are, I am now receiving this error:

              syntax error, unexpected '}' in /home/content/m/i/d/mideasthonors/html/admintaskstable .php on line 23

              I thought these brackets were necessary to close the connection?

              Comment

              • dlite922
                Recognized Expert Top Contributor
                • Dec 2007
                • 1586

                #8
                Originally posted by mideastgirl
                So I put the quote at the end of line 26, which solved that problem. Now at the bottom of the script where the last 2 curly brackets are, I am now receiving this error:

                syntax error, unexpected '}' in /home/content/m/i/d/mideasthonors/html/admintaskstable .php on line 23

                I thought these brackets were necessary to close the connection?
                It's not that hard. All opened brackets must have closing brackets.

                Try this code:

                Code:
                <?php
                mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
                mysql_select_db($dbname);
                 
                $query = 'SELECT * FROM $usertable';
                $result = mysql_query($query);
                
                if($result) 
                {
                    while($row = mysql_fetch_array($result))
                    {
                        $name = $row['$yourfield'];
                        echo 'Name: '.$name;
                 
                		mysql_select_db("mideasthonors2");
                 
                		$sql="INSERT INTO $usertable (Name, Address 1, Address 2, City, State, Zip Code, Website, Contact Name, Position, Phone Number, Email Address) VALUES {$_POST['name']},{$_POST['address 1']},{$_POST['address 2']},{$_POST['city']},{$_POST['state']},{$_POST['zip code']},{$_POST['website']},{$_POST['contact name']},{$_POST['position']},{$_POST['phone number']},{$_POST['email address']}";
                 
                		if (!mysql_query($sql,$con))		 
                		{
                			die('Error: ' . mysql_error());
                		}
                		 
                		echo '1 record added'; 
                    }
                }
                
                mysql_close($con);    // should be at the same level as your connect().

                Comment

                • mideastgirl
                  New Member
                  • Jun 2009
                  • 65

                  #9
                  Apparently it is more difficult for me than others:(

                  I tried the code you gave me and this is the error I received:

                  Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /home/content/m/i/d/mideasthonors/html/admintaskstable .php on line 28

                  You did not include the database connection which I added on line 2, which gave me an error that no connection was possible, or something to that degree. And now it is giving me the error above. This is the code I am using:
                  [code=php]
                  <?php
                  include ("admintasks-dbcon.php");
                  mysql_connect($ hostname,$usern ame, $password) OR DIE ('Unable to connect to database! Please try again later.');
                  mysql_select_db ($dbname);

                  $query = 'SELECT * FROM $usertable';
                  $result = mysql_query($qu ery);

                  if($result)
                  {
                  while($row = mysql_fetch_arr ay($result))
                  {
                  $name = $row['$yourfield'];
                  echo 'Name: '.$name;

                  mysql_select_db ("mideasthonors 2");

                  $sql="INSERT INTO $usertable (Name, Address 1, Address 2, City, State, Zip Code, Website, Contact Name, Position, Phone Number, Email Address) VALUES {$_POST['name']},{$_POST['address 1']},{$_POST['address 2']},{$_POST['city']},{$_POST['state']},{$_POST['zip code']},{$_POST['website']},{$_POST['contact name']},{$_POST['position']},{$_POST['phone number']},{$_POST['email address']}";

                  if (!mysql_query($ sql,$con))
                  {
                  die('Error: ' . mysql_error());
                  }

                  echo '1 record added';
                  }
                  }
                  mysql_close($co n);
                  ?>[/code]
                  Last edited by Atli; Jun 24 '09, 06:43 PM. Reason: Added [code] tags.

                  Comment

                  • code green
                    Recognized Expert Top Contributor
                    • Mar 2007
                    • 1726

                    #10
                    I find it hard to believe you do not understand.
                    The error message tells you exactly what is wrong.
                    Code:
                    mysql_close($con);
                    Where is the resource $con?
                    Is it in
                    include ("admintasks-dbcon.php");
                    If not then it just comes randomly into play half way through your script.
                    Even so it is not a critical error so it should still work.
                    I would have expected a more serious error earlier because the mysterious $con
                    is used here
                    Code:
                     if (!mysql_query($sql,$con))
                    but php is good at guessing which database you mean

                    Comment

                    • mideastgirl
                      New Member
                      • Jun 2009
                      • 65

                      #11
                      Thank you for your help. I deleted the start session at the top of this page and no error message came up, but it still will not go to the next page of admintasks.php which is where it should go to after logging in. Here is my script:

                      Code:
                      <?php
                      include("admin-dbcon.php");
                      
                      //Check to see if the username and password is valid (if it is in the database)
                      $validate = mysql_query("select * from admin_login where username = '$_POST[username]' and password = '$_POST[password]'"); 
                      
                      $isvalid=mysql_num_rows($validate);
                      
                      //if valid login send on, if not send to error page
                      if ($isvalid != 0) {
                      	$_SESSION['username'] = $_POST[username];
                      	while ($row=mysql_fetch_array($validate)){
                      		$_SESSION['userid']=$row["ID"];
                      	}
                       	header("Location: admintasks.php");
                      } 
                      //else {
                      //	header("Location: login-error.php");
                      //}
                      ?>
                      Last edited by Markus; Jun 25 '09, 01:18 PM. Reason: Added [code] tags.

                      Comment

                      • mideastgirl
                        New Member
                        • Jun 2009
                        • 65

                        #12
                        Headers already sent

                        I am still getting an error message when I try to login from page www.mideasthonors.org/adminlogin.php this is the error I am receiving:

                        Warning: Cannot modify header information - headers already sent by (output started at /home/content/m/i/d/mideasthonors/html/adminloginproce ss.php:1) in /home/content/m/i/d/mideasthonors/html/adminloginproce ss.php on line 15
                        This is the code I am using:
                        [code=php]
                        <?php
                        include("admin-dbcon.php");

                        //Check to see if the username and password is valid (if it is in the database)
                        $validate = mysql_query("se lect * from admin_login where username = '$_POST[username]' and password = '$_POST[password]'");

                        $isvalid=mysql_ num_rows($valid ate);

                        //if valid login send on, if not send to error page
                        if ($isvalid != 0) {
                        $_SESSION['username'] = $_POST[username];
                        while ($row=mysql_fet ch_array($valid ate)){
                        $_SESSION['userid']=$row["ID"];
                        }
                        header("Locatio n: admintasks.php" );
                        }
                        //else {
                        // header("Locatio n: login-error.php");
                        //}
                        ?>
                        [/code]

                        Comment

                        • code green
                          Recognized Expert Top Contributor
                          • Mar 2007
                          • 1726

                          #13
                          Cannot modify header information - headers already sent by
                          This generally means the browser has already output something or recieved HTML headers before receiving some header information.

                          I am not very experienced with header stuff but session_start must be called before any other output to the browser, including header().

                          What is admin-dbcon.php up to?

                          Just check you have no white space either side of <?php, this has got me a few times

                          Comment

                          • mideastgirl
                            New Member
                            • Jun 2009
                            • 65

                            #14
                            No white space on any pages!

                            This is the code in admin-dbcon.php:

                            [code=php]
                            <?php
                            $dbHost = 'mideasthonors. db.4381361.host edresource.com' ;
                            $dbName = 'mideasthonors' ;
                            $dbUser = 'xxx';
                            $dbPass = 'xxx';

                            mysql_connect(" $dbHost","$dbUs er","$dbPass" ) or die("Error: Unable to connect to database! Please try again later.");

                            //Select the database we want to use
                            mysql_select_db ($dbName) or die("Error: Could not select database");
                            ?>
                            [/code]

                            I have not opened a session in either one or a header at the top. So I will re-add session start () and see what happens

                            Comment

                            • mideastgirl
                              New Member
                              • Jun 2009
                              • 65

                              #15
                              I feel like code needs to be in adminlogin.php and admintasks.php to get the two to connect to the actual loginprocess.ph p page. I am trying to get from adminlogin.php to admintasks.php through a login process which is in loginprocess.ph p. The code to connect to mysql is in admin-dbcon.php...in other words, I feel like all five pages should be linked someone??? Maybe I am wrong...but I am literally only a week into all the php stuff and have looked up how to do what I have so far online. I really do appreciate the help you all are giving.

                              Comment

                              Working...