所以我在php
中为登录页面编写了这段代码<?php
function SignIn() {
session_start(); //starting the session for user profile page
if(!empty($_POST['username'])) //checking the 'user' name which is from Sign-In.html, is it empty or have some text
{
$query = mysql_query("SELECT * FROM register where Username = '$_POST[username]' AND Password = '$_POST[password]'") or die(mysql_error());
$row = mysql_fetch_array($query) or die(mysql_error());
if(!empty($row['Username']) AND !empty($row['Password'])) {
if($password = $row['Password']) {
$_SESSION['Username'] = $row['Password'];
echo "welcome ".$row['Name'];
}
if ($password != $row['Password']) {
echo "Something went wrong!" ;
}
}
}
}
if(isset($_POST['submit'])) {
SignIn();
}
?>
我的问题是,虽然登录部分工作正常(当我给出正确的密码时),当我给出错误的密码时,它不会显示我将其设置为回显但显示空白页面的消息....我尝试了很多次但没有结果......有人可以给我一些帮助吗?
答案 0 :(得分:2)
您的第一个IF
语句将$password
设置为$row['password']
值:
if($password = $row['Password'])
当您的第二个if语句开始时,$password
与$row['password']
相同且条件等于false:
if($password != $row['Password'])
解决方案是将第一个if语句更改为比较等于:
if($password == $row['Password'])
请注意,有两个=
个符号?这告诉PHP检查它们是否都是相同的值。
答案 1 :(得分:1)
你似乎错过了一个=符号:
if($password = $row['Password'])
因此你没有做比较,而是做作业。我希望这应该是:
if($password == $row['Password'])
答案 2 :(得分:1)
试试这个
function SignIn() {
session_start(); //starting the session for user profile page
if(!empty($_POST['username'])) //checking the 'user' name which is from Sign-In.html, is it empty or have some text
{
$query = mysql_query("SELECT * FROM register where Username = '$_POST[username]' AND Password = '$_POST[password]'") or die(mysql_error());
$row = mysql_fetch_array($query) or die(mysql_error());
if(!empty($row['Username']) && !empty($row['Password'])) {
if($password == $row['Password'])
{
$_SESSION['Username'] = $row['Password'];
echo "welcome ".$row['Name'];
}
else
echo "Something went wrong!" ;
}
}
}
答案 3 :(得分:1)
如果条件必须在第一个if条件之外,则为第二个。
您还要将密码分配给$_SESSION['Username']
而不是$row['Username']
。我编辑了代码..见下文
<?php
function SignIn() {
session_start(); //starting the session for user profile page
if(!empty($_POST['username'])) //checking the 'user' name which is from Sign-In.html, is it empty or have some text
{
$query = mysql_query("SELECT * FROM register where Username = '$_POST[username]' AND Password = '$_POST[password]'") or die(mysql_error());
$row = mysql_fetch_array($query) or die(mysql_error());
if(!empty($row['Username']) AND !empty($row['Password'])) {
if($_POST['password'] = $row['Password'])
$_SESSION['Username'] = $row['Username'];
echo "welcome ".$row['Username'];
}
if ($_POST['password'] != $row['Password']) {
echo "Something went wrong!" ;
}
}
}
if(isset($_POST['submit'])) {
SignIn();
}
?>