PHP代码中意外的T变量,无法弄清楚原因

时间:2013-04-14 18:37:51

标签: php syntax-error

我无法看到我的错误来自哪里,我检查了分号,看不到任何遗漏。我是相当新的PHP,非常感谢帮助。实际错误如下:

  

解析错误:语法错误,意外的T_VARIABLE   第54行/home/sam/public_html/query.php

以下是第54行:

$sql = "UPDATE Users SET Datecreated='".$date."' WHERE Username='".$usernamec."'";

这是完整的代码:

<?php  session_start();

include 'connect.php';

echo "Hello World!";

echo '$_SESSION[page]';

$_SESSION['loggedon']=12345;
$username=$_POST['username'];
$password=$_POST['password'];

$sql = "SELECT * FROM 'Users' WHERE 'Username'='".$username."'";
$result = @mysql_query( $sql );
while ($row= @mysql_fetch_assoc($result))
    {
    if (($username=$row['Username'])&&($password=$row['Password']))
    {
        $_SESSION['loggedon']=$row['AccessLevel'];
        $_SESSION['userid']=$row['UserID'];}}


if ($_SESSION['loggedon']==12345)
    {
    $sql = "SELECT * FROM 'RedundantUsers' WHERE 'Username'='".$username."'";
    $result = @mysql_query( $sql );
    while ($row= @mysql_fetch_assoc($result))
        {   
            $_SESSION['loggedon']=$row['AccessLevel'];
            $_SESSION['userid']=$row['UserID'];


            $userid    =$row['UserID'];
            $forename  =$row['Forename'];
            $surname   =$row['Surname'];
            $email     =$row['Email'];
            $usernamec =$row['Username'];
            $access    =$row['AccessLevel'];


            $sql = "INSERT INTO 'Users' ('UserID', 'Forename', 'Surname', 'Email', 'Username', 'AccessLevel')
                    VALUES ('$userid', '$forename', '$surname', '$email', '$usernamec', '$access')";

                if (!@mysql_query($sql))    
                    {
                    die('Error: ' . mysql_error());
                        }
               $sql = "DELETE * FROM 'RedundantUsers' WHERE 'Username'='".$usernamec."'"; }}

    date_default_timezone_set('UTC');

        $date = date("Y-m-d H:i:s");    

            $sql = "UPDATE 'Users' SET 'Datecreated'='".$date."' WHERE 'Username'='".$usernamec."'";

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


                    echo $_SESSION['loggedon']; 

header('Location: /'.$_SESSION['page']);

?>

如果有进一步的帮助,请联系connect.php:

<?php
$dbhost = '*****';
$dbuser = '*****'; 
$dbpass = '*****';  
$conn = @mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to local MySQL')
$dbname = '******';
@mysql_select_db($dbname);      
?>

2 个答案:

答案 0 :(得分:1)

在第54行(发生错误的地方)

更改

echo $_SESSION[loggedon];

echo $_SESSION['loggedon'];

表示您忘记在会话值中包含'。除非值是定义的常量,否则应始终包含单引号。

<强>更新


<?php  session_start();

include 'connect.php';

echo "Hello World!";

echo $_SESSION['page'];

    $_SESSION['loggedon']=12345;
    $username=$_POST['username'];
    $password=$_POST['password'];

    $sql = "SELECT * FROM Users WHERE Username='".$username."'";
    $result = mysql_query($sql) or die(mysql_error());
    while ($row= mysql_fetch_array($result))
        {

        if (($username=$row['Username'])&&($password=$row['Password']))
        {
            $_SESSION['loggedon']=$row['AccessLevel'];
            $_SESSION['userid']=$row['UserID'];}}


    if ($_SESSION['loggedon']==12345)
        {
        $sql = "SELECT * FROM RedundantUsers WHERE Username='".$username."'";
        $result = mysql_query($sql);
        while ($row= mysql_fetch_array($result))
            {   
                $_SESSION['loggedon']=$row['AccessLevel'];
                $_SESSION['userid']=$row['UserID'];


                $userid    =$row['UserID'];
                $forename  =$row['Forename'];
                $surname   =$row['Surname'];
                $email     =$row['Email'];
                $usernamec =$row['Username'];
                $access    =$row['AccessLevel'];




                $sql = "INSERT INTO Users (UserID, Forename, Surname, Email, Username, AccessLevel)
                        VALUES ('$userid', '$forename', '$surname', '$email', '$usernamec', '$access')";

                    if (!mysql_query($sql))    
                        {
                        die('Error: ' . mysql_error());
                            }
                   $sql = "DELETE * FROM RedundantUsers WHERE Username='".$usernamec."'"; }}

        date_default_timezone_set('UTC');

            $date = date("Y-m-d H:i:s");  
            global $usernamec;          

                $sql = "UPDATE Users SET Datecreated='".$date."' WHERE Username='".$usernamec."'";

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


                        echo $_SESSION[loggedon];   

    header('Location: /'.$_SESSION[page]);

    ?>

答案 1 :(得分:1)

这里有一些明显的问题:

  1. 您正在使用mysql_*方法,而应使用MySQLi。
  2. 您无法确保对SQL查询中包含的字符串进行清理,从而为SQL注入做好准备。
  3. 数组的键应该是字符串,这意味着将它们放在单引号中。
  4. mysql_fetch_array切换到mysql_fetch_assoc将返回一半的数据量,但仍然可以正常工作 - 无需无缘无故地增加内存使用量。
  5. 使用反引号(`)表示您的表名和列名,这有助于保持SQL整洁,但也避免与保留字等发生任何潜在冲突。
  6. 最终的事情如下:

    $row[UserID];
    

    应该变成:

    $row['UserID'];
    

    您在代码中使用$_SESSION$_POST或其他数组的位置相同。

    另外,请务必查看MySQLi上的this tutorial,尤其是有关转义字符串的部分!

    修改

    您在if语句中使用声明,而不是比较:

    if (($username=$row[Username])&&($password=$row[Password]))
    

    应该是:

    if (($username==$row[Username])&&($password==$row[Password]))
    

    仔细检查所有if语句。