I am making a website and I have some HTML code and PHP code in a file like this

<html>
    some code
<?php
function login()
{
if (empty ($_POST['username']))
{
    return false;
}
if (empty ($_POST['password']))
{
    return false;
}
$username = trim ($_POST['username']);
$password = trim ($_POST['password']);
$scrambled = md5 ($password . 'foo');
$link = mysqli_connect('localhost', 'root', 'password');
if (!$link)
{
    $error = "Unable to connect to the database server";
    include 'error.html.php';
    exit ();
}
if (!mysqli_set_charset ($link, 'utf8'))
{
    $error = "Unable to set database connection encoding";
    include 'error.html.php';
    exit ();
}
if (!mysqli_select_db ($link, 'foo'))
{
    $error = "Unable to locate the foo database";
    include 'error.html.php';
    exit ();
}
$sql = "SELECT COUNT(*) FROM admin WHERE username = '$username' AND password = '$scrambled'";
$result = mysqli_query ($link, $sql);
if (!$result)
{
    return false;
    exit ();
}
$row = mysqli_fetch_array ($result);
if ($row[0] > 0)
{
    return true;
}
else
{
    return false;
}
}
if (login())
{?>
<h2>Welcome Administrator</h2>
<a href=\"upload.php\">Upload Files</a>
<br />
<a href=\"points.php\">Edit Points Tally</a>
<?php}
else
{
echo "Incorrect login details. Please login";
}
?>
some more html code
</html>

When i run it, it says Parse error: syntax error, unexpected end of file in the line where I have written . What's the problem?

share|improve this question

60% accept rate
I would imagine you have an error with some of your PHP code... which you haven't posted. – Yacoby Jul 14 at 9:22
It sounds like you have a missing '}', ';' or bracket but it would help if you posted you whole php code – Ryanc1256 Jul 14 at 9:23
Please post a complete example – phihag Jul 14 at 9:23
I have added the code – Pratyush Nalam Jul 14 at 9:25
As @Ryanc1256 mentioned, it is most probably a case of missing bracket. Do include the actual code with the question, if you really want a solution. – Joyce Babu Jul 14 at 9:25
show 1 more comment
feedback

3 Answers

up vote 1 down vote accepted

You should avoid this (at the end of your code):

{?>

and this :

<?php}

You shouldn't put brackets directly close to the open/close php tag, but separate with space:

{ ?>
<?php {
share|improve this answer
Thanks!!! This worked!! Thank you – Pratyush Nalam Jul 14 at 9:34
feedback

OK i saw some error's which I have fixed so... from what i got it was this error part

if (login())
{?>
<h2>Welcome Administrator</h2>
<a href=\"upload.php\">Upload Files</a>
<br />
<a href=\"points.php\">Edit Points Tally</a>
<?php}
else
{
echo "Incorrect login details. Please login";
}

your new code This is how i would do it any way

<html>
    some code
<?php
function login()
{
    if (empty ($_POST['username']))
    {
        return false;
    }
    if (empty ($_POST['password']))
    {
        return false;
    }
    $username = trim ($_POST['username']);
    $password = trim ($_POST['password']);
    $scrambled = md5 ($password . 'foo');
    $link = mysqli_connect('localhost', 'root', 'password');
    if (!$link)
    {
        $error = "Unable to connect to the database server";
        include 'error.html.php';
        exit ();
    }
    if (!mysqli_set_charset ($link, 'utf8'))
    {
        $error = "Unable to set database connection encoding";
        include 'error.html.php';
        exit ();
    }
    if (!mysqli_select_db ($link, 'foo'))
    {
        $error = "Unable to locate the foo database";
        include 'error.html.php';
        exit ();
    }
    $sql = "SELECT COUNT(*) FROM admin WHERE username = '$username' AND password = '$scrambled'";
    $result = mysqli_query ($link, $sql);
    if (!$result)
    {
        return false;
        exit ();
    }
    $row = mysqli_fetch_array ($result);
    if ($row[0] > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
if (login())
{
echo '<h2>Welcome Administrator</h2>
<a href=\"upload.php\">Upload Files</a>
<br />
<a href=\"points.php\">Edit Points Tally</a>';
}
else
{
    echo "Incorrect login details. Please login";
}
?>
some more html code
</html>
share|improve this answer
Well the thing is inside if (login ()), I have lot of code to write, this is just a sample. Anyway the first answer here has solved it. I didn't know that a space has to be put. Thanks for the help! – Pratyush Nalam Jul 14 at 9:36
yer just helping anyway yer i did get that but i just like to do it that way. And yer sure – Ryanc1256 Jul 14 at 9:38
feedback

I have solved your error check it

<?php
function login()
{
    if (empty ($_POST['username']))
    {
        return false;
    }
    if (empty ($_POST['password']))
    {
        return false;
    }
    $username = trim ($_POST['username']);
    $password = trim ($_POST['password']);
    $scrambled = md5 ($password . 'foo');
    $link = mysqli_connect('localhost', 'root', 'password');
    if (!$link)
    {
        $error = "Unable to connect to the database server";
        include 'error.html.php';
        exit ();
    }
    if (!mysqli_set_charset ($link, 'utf8'))
    {
        $error = "Unable to set database connection encoding";
        include 'error.html.php';
        exit ();
    }
    if (!mysqli_select_db ($link, 'foo'))
    {
        $error = "Unable to locate the foo database";
        include 'error.html.php';
        exit ();
    }
    $sql = "SELECT COUNT(*) FROM admin WHERE username = '$username' AND password = '$scrambled'";
    $result = mysqli_query ($link, $sql);
    if (!$result)
    {
        return false;
        exit ();
    }
    $row = mysqli_fetch_array ($result);
    if ($row[0] > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
if (login())
{?>
    <h2>Welcome Administrator</h2>
    <a href=\"upload.php\">Upload Files</a>
    <br />
    <a href=\"points.php\">Edit Points Tally</a>
    <?php
}
else
{
    echo "Incorrect login details. Please login";
}
?>
share|improve this answer
Isn't this the same? – Pratyush Nalam Jul 14 at 9:34
it is the exact same, I checked – Ryanc1256 Jul 14 at 9:35
I does not change any code only solved your errors – Harry Jul 14 at 9:37
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.