我是PHP脚本的新手,来自Java背景。这是一个微不足道的事情,现在变成了我的脑力激荡。所以这就是问题所在,我为变量分配了一些值,当尝试在if / else语句中使用该值时,变量实际上不具有先前赋值的值。这是代码: -
<?php
session_start();
$email = $_POST["Email"];
$password = $_POST["Password"];
$db_username="root";
$db_password="root";
$database="mydb";
$localhost = "mysql";
$con = mysql_connect($localhost,$db_username,$db_password);
mysql_select_db($database,$con) or die( "Unable to select database");
$query = "select * from photobook.users where email ='$email' and password ='$password';" ;
$result = mysql_query($query);
$num=mysql_num_rows($result);
if($num == 1){
while($row = mysql_fetch_array($result))
{
$_SESSION['email'] = $row['email'];
$_SESSION['username'] = $row['username'];
}
header("location: home.php");
}
else{
include "photoBookProtocol.php";
print "<br>email value after photobookprotocol file include is $email";
print "<br>password value after photobookprotocol file include is $password";
$obj=new Protocol();
$var = $obj->loginCheck($email,$password);
print "value of var received is $var";
if($var == 0){
session_destroy();
print "<br>user does not exist";
//header("location: login.php");
}
else{
$_SESSION['email'] = $var[0];
$_SESSION['username'] = $var[1];
print "<br>user exists";
header("location: home.php");
}
}
mysql_close($con);
?>
因此,当我在“else”子句中传递loginCheck($ email,$ password)中的$ email和$密码时,没有任何内容传递。知道为什么会这样吗?
答案 0 :(得分:1)
您的变量范围没有任何问题,因此:
loginCheck()
正在接收正确的变量,但函数作为旁注,由于您的脚本依赖于POST数据,因此您应该有条件在继续之前检查所需数据是否存在:
if(isset($_POST['Email'], $_POST['Password']))
{
// something posted
}