我无法看到我的错误来自哪里,我检查了分号,看不到任何遗漏。我是相当新的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);
?>
答案 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)
这里有一些明显的问题:
mysql_*
方法,而应使用MySQLi。mysql_fetch_array
切换到mysql_fetch_assoc
将返回一半的数据量,但仍然可以正常工作 - 无需无缘无故地增加内存使用量。最终的事情如下:
$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
语句。