2015-01-04 74 views
1
<!DOCTYPE HTML> 
<html> 
<head> 
<title>Sign-In</title> 
</head> 
<body id="body-color"> 
<div id="Sign-In"> 
<form method="POST" action="connectivity.php"> 
User <br><input type="text" name="user" size="40"><br> 
Password <br><input type="password" name="pass" size="40"><br> 
<input id="button" type="submit" name="submit" value="Log-In"> 
</form> 
</fieldset> 
</div> 
</body> 
</html> 

以上是我用於簡單用戶登錄的表單的html代碼。在PHP代碼中的其他塊不起作用

<?php 
define('DB_HOST', 'localhost:3306'); 
define('DB_NAME', 'sample'); 
define('DB_USER','user'); 
define('DB_PASSWORD','passwd'); 

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error()); 
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error()); 
function SignIn() 
{ 
session_start(); //starting the session for user profile page 
if(!empty($_POST['user'])) //checking the 'user' name which is from Sign-In.html, is it empty or have some text 
{ 
    $query = mysql_query("SELECT * FROM UserName where userName = '$_POST[user]' AND pass = '$_POST[pass]'") or die(mysql_error()); 
    $row = mysql_fetch_array($query) or die(mysql_error()); 
    if(!empty($row['userName']) AND !empty($row['pass'])) 
    { 
     $_SESSION['userName'] = $row['pass']; 
     echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE..."; 

    } 
    else 
    { 
     echo "SORRY... YOU ENTERED WRONG ID AND PASSWORD... PLEASE RETRY..."; 
    } 
} 
} 
if(isset($_POST['submit'])) 
{ 
    SignIn(); 
} 

?> 

這是我用來驗證用戶登錄的php腳本。但是,當我輸入之前存儲在數據庫中的正確用戶名和密碼時,上述php代碼正確響應。但是當我嘗試輸入錯誤的用戶名和密碼時,其他部分不起作用。相反,它顯示一個白色的屏幕。我在哪裏犯了錯誤?

任何人都可以幫助我解決這個問題嗎?

回答

1

,如果沒有更多的行

所以檢查FALSE不爲空,則返回一個字符串數組對應於提取行,或 FALSE。

if(($row['userName'])!==FALSE AND ($row['pass'])!==FALSE) 
0

mysql_fetch_array返回false時,您的代碼失敗。簡單地說,從if語句中刪除empty,然後您將檢查查詢是否返回了一條或多條記錄並填充了該數組。