2015-02-24 90 views
-1

我目前正努力讓我的PHP登錄工作。 Iv使用了一個分離關注點來構建我的PHP。PHP使用mySQL登錄

Iv已經在我的數據庫中設置了一個帶有用戶名,用戶名和密碼值的用戶表。

我不認爲有連接到我的數據庫有問題,因爲我在其他項目中使用相同的代碼,它在那裏工作。

這是我user_repository.php:

<?php 
 
require_once "database.php"; 
 

 
function authenticate_user($username, $password, &$error) { 
 

 
\t $sql = "select * from Users where username= '${username}'"; 
 
\t $sql .=" and password='{password}'"; 
 
\t $result = query($sql); 
 

 
\t if ($result->num_rows!=1) { 
 

 
\t \t $error = "Username or password was incroeect."; 
 
\t \t return null; 
 

 
\t } 
 

 
return $result-> fetch_assoc(); 
 

 
} 
 

 
function get_user_by_id($id, &$error) { 
 

 
\t $sql = "select * from Users where id={$id} limit 1;"; 
 
\t $result = query($sql); 
 

 
\t return $result->fetch_assoc(); 
 

 

 

 
} 
 

 
?>

這是我爲database.php:

<?php 
 

 
\t define("SQLHOST", "localhost"); 
 
\t define("SQLUSER", "b3006796"); 
 
\t define("SQLDB", "b3006796_db3"); 
 
\t define("SQLPASSWORD", "*******"); 
 

 
\t function connect_to_database() { 
 

 
\t \t $mysqli = new mysqli(SQLHOST, SQLUSER, SQLPASSWORD, SQLDB); 
 

 
\t \t if($mysqli->connect_errno) { 
 

 
\t \t \t echo "failed to connect to mysql: ".$mysqli->connect_errno; 
 
\t \t \t exit(); 
 

 

 
\t \t } 
 

 
\t \t return $mysqli; 
 
\t } 
 

 
\t function query ($sql) { 
 

 
\t \t $mysqli = connect_to_database(); 
 

 
\t \t $result = $mysqli->query($sql); 
 
\t \t if (!$result) { 
 
\t \t \t echo "failed to run query: ".$mysqli->error; 
 
\t \t \t exit(); 
 

 

 

 
} 
 

 
return $result; 
 

 
} 
 
?>

這是我的index.php :

<?php 
 

 
\t session_start(); 
 
\t require_once "user_repository.php"; 
 

 
\t $error = null; 
 

 
\t if (isset ($_POST["username"]) && isset($_POST["password"])) { 
 
\t \t $username = $_POST ["username"]; 
 
\t \t $password = $_POST ["password"]; 
 

 

 
\t \t // Get the assoc array for the user. 
 
\t \t $user = authenticate_user($username, $password, $error); 
 

 
\t \t //No error means valid password here. 
 
\t \t if (!$error) { 
 
\t \t \t $_SESSION['currentUser'] = $user['id']; 
 
\t \t \t header("location:account.php"); 
 
\t \t \t exit(); 
 

 
\t \t } 
 

 

 
\t \t } 
 

 
?> 
 

 
<html> 
 
<head> 
 
\t <title> DIWA Login </title> 
 
</head> 
 
<body> 
 
\t <h1>login</h1> 
 
\t <form method="post"> 
 
\t \t Username:<input name="username"/> 
 
\t \t Password:<input name="password" type="password"/> 
 
\t \t <input type="submit"/> 
 
\t \t <?php if ($error);?> 
 
\t \t <p><?php echo $error; ?></p> 
 

 
\t </form? 
 
</body> 
 
</html>

這是我Account.php:

<?php 
 
\t session_start(); 
 
\t require_once "user_repository.php"; 
 

 
\t $error = null; 
 

 
\t if (!isset($_SESSION["currentUser"])) { 
 
\t header ("Location: login.php"); 
 
\t exit(); 
 
\t } 
 

 
\t $user = get_user_by_id($_SESSION["currentUser"], $error); 
 
\t ?> 
 

 
\t <html> 
 
\t <head> 
 
\t \t <title> DIWA Account </title> 
 
\t </head> 
 
\t <body> 
 
\t \t <h1> Account </h1> 
 
\t \t <p> User ID: <?php echo $user["id"]; ?></p> 
 
\t </body> 
 
\t </html>

感謝

+0

so what your question – dsharew 2015-02-24 09:57:24

+1

$ sql。=「and password = '{密碼}'」; - 你省略了「$」 - $ password – 2015-02-24 09:58:28

回答

0

如果你的問題是,你不能登錄,然後Ť這裏至少有一個錯誤:

`$sql .=" and password='{password}'";` 

你在 '密碼' 省略 「$」,它應該是

$sql .=" and password='{$password}'"; 

另外:

$sql = "select * from Users where username= '${username}'"; 

應該是:

$sql = "select * from Users where username= '{$username}'"; 
+0

非常感謝!效果很好 – 2015-02-24 10:42:39