2014-04-02 103 views
1

我想創建一個登錄功能與PHP。登錄功能與PHP的mysql錯誤

直到現在,這是我得到的。

<?php 
include("dbconnection.php"); 
//select a database to work with 
$mysqli->select_db("eamunter_opskrift"); 
Echo ("Selected the eamunter_opskrift database...<BR><BR>"); 
// get the username and password from the login form 
$username=$_POST['username']; 
$password=$_POST['password']; 

//Protect username and password 
$username = stripslashes($username); 
$password = stripslashes($password); 
$username = mysql_real_escape_string($username); 
$password = mysql_real_escape_string($password); 

$query ="SELECT * FROM brugerregistration WHERE username='$username' and password='$password'"; 
$result = $mysqli->query($query); 
$count=mysqli_num_rows($result); 
// If result matched $username and $password, the count of table rows should equal 1 
if($count==1){ 
// Register session variables and redirect the user to "login_success.php" page 
session_start(); 
$_SESSION['username']= $username; 
header("location:login_success.php"); 
} 
else { 
echo "Wrong Username or Password"; 
} 
?> 

我不斷收到同樣的錯誤 我想不出是什麼問題...我dbconnection.php工作的其他任何地方。

Selected the eamunter_opskrift database... 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in /home/www/.dk/reg/post_login.php on line 13 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: No such file or directory in /home/www/.dk/reg/post_login.php on line 13 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/www/.dk/reg/post_login.php on line 13 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in /home/www/.dk/reg/post_login.php on line 14 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: No such file or directory in /home/www/.dk/reg/post_login.php on line 14 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/www/.dk/reg/post_login.php on line 14 

Wrong Username or Password 

任何人都可以幫我嗎?

如果重要我也可以上傳表格,但並不認爲它很重要。

回答

3

更換mysql_real_escape_string$mysqli->real_escape_string

(另外,你不需要stripslashes電話,real_escape_string應該做的一切,爲你)

+0

很感謝:-) 你知道爲什麼它會在鏈接末尾寫'post_login.php?username = user&password = 1234&Submit = Login'。鏈接中的密碼不是很聰明... –

+1

可能是因爲您的'操作將被設置爲GET而不是POST。 –

+1

在您的HTML表單中,添加'method =「post」'以便讀取'

3

您在混合使用mysql_*mysqli_*函數。

應該

$username = $mysqli->real_escape_string($username); 
$password = $mysqli->real_escape_string($password); 
+1

由於沒有注意到。現在我只需要讓它與數據庫一起工作:-)現在只能獲得'選中eamunter_opskrift數據庫... 錯誤的用戶名或密碼'。 –

1

試試:

<?php 
    include("dbconnection.php"); 
    //select a database to work with 
    $mysqli->select_db("eamunter_opskrift"); 
    Echo ("Selected the eamunter_opskrift database...<BR><BR>"); 
    // get the username and password from the login form 
    $username=$_POST['username']; 
    $password=$_POST['password']; 

    //You should use your object and current connection 
    $username = $mysqli->real_escape_string($username); 
    $password = $mysqli->real_escape_string($password); 

    $query ="SELECT * FROM brugerregistration WHERE username='$username' and password='$password'"; 
    $result = $mysqli->query($query); 
    $count=mysqli_num_rows($result); 
    // If result matched $username and $password, the count of table rows should equal 1 
    if($count==1){ 
    // Register session variables and redirect the user to "login_success.php" page 
    session_start(); 
    $_SESSION['username']= $username; 
    header("location:login_success.php"); 
    } 
    else { 
    echo "Wrong Username or Password"; 
    } 
    ?> 
1

看到t他的mysql_real_escape_string

DOC替換:

$username = mysql_real_escape_string($username); 
$password = mysql_real_escape_string($password); 

由:

$username = $mysqli->real_escape_string($username); 
$password = $mysqli->real_escape_string($password);