我正在建立一个网站,其中有一个用户数据库,用户可以登录并注册。当我测试PHP代码登录时,它说"解析错误"一次和invalid { in line 16
。我的代码有什么问题?
<?php
$user = $_REQUEST['user'];
$pass = $_REQUEST['pass'];
$con = mysql_connect("localhost","root","xxxxxxxxxx") or die('Could not connect:'.mysql_error());
$test = mysql_select_db("users",$con)or die("unable to connect");
$result = "SELECT * FROM users where username='$user' AND password='$pass'";
$alpha = @mysql_query($result,$con);
if($alpha==1)
{
$expire=time()+60*60*24*30;
setcookie("id",$row['id'],$expire);
echo "Logged in. as <b>".$row['username']."</b>";
$userID = $row['id'];
header("index map.html")
}
else
{
echo "<b>Username or password is wrong</b><br><br>";
header("index.html")
}
mysql_close($con);
?>
这里你的PHP有几个问题。
-
header
需要一个分号关闭 -
header
需要一个位置设置。即header('location: index.html');
你应该替换
header("index map.html")
header("location: map.html");
还需要替换
header("index.html")
header("Location: index.html");
怎么回事?row是从哪里来的?你没有调用任何mysql_fecth_array或一些相关的东西来遍历结果成一行。至少有u个代码使用过
$row = mysql_fetch_result/array($result,$conn);
然后使用row作为我的建议…
if($alpha==1)
{
$expire=time()+60*60*24*30;
setcookie("id",$row['id'],$expire);
echo "Logged in. as <b>".$row['username']."</b>";
$userID = $row['id'];
header("index map.html")
}
首先你的代码是非常不安全的!我们可以很容易地做一些SQL注入。尝试至少使用:mysql_real_escape
。(如何在PHP中使用mysql_real_escape_string函数)。在我看来,使用PDO会更好。
你忘记了
中的点 $result = "SELECT * FROM users WHERE username='.$user.' AND password='.$pass.'";
(line 7)
您忘记放置分号和字段关键字(第21行)应该是header("location: index.html");
问好。
你的header()函数调用不正确,用
改正它header("location:index-map.html");