2014-02-10 63 views
0

嗨,由於某種原因我不知道我繼續收到以下錯誤:警告:mysql_real_escape_string()[function.mysql-real-escape-string]:訪問拒絕用戶'a8221325'@'localhost'(使用密碼:否)

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'a8221325'@'localhost' (using password: NO) in /home/a8221325/public_html/add.php on line 11 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/a8221325/public_html/add.php on line 11 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'a8221325'@'localhost' (using password: NO) in /home/a8221325/public_html/add.php on line 12 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/a8221325/public_html/add.php on line 12 

從研究,我已經做了我覺得這個問題是在我連接到我的數據庫的方式,但我不能完全肯定。如果有人能幫我讓我的代碼工作,那就太棒了! 到目前爲止,我的網站包含兩個頁面,測試並添加這裏是他們的代碼:

的test.html:

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Untitled Document</title> 
</head> 

<body> 
<form action="add.php" method="post" name="form1" id="form1"> 
    <label for="name">Text Field:</label> 
    <input type="text" name="name" id="name"> 
    <label for="password">Text Field:</label> 
    <input type="text" name="password" id="password"> 
    <input type="submit" name="submit" id="submit" value="Submit"> 
</form> 
</body> 
</html> 

add.php:

<?php 
$con=mysqli_connect("mysql1.000webhost.com","a8221325_admin","**************","a8221325_prom"); 

// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

$escapedName = mysql_real_escape_string($_POST['name']); # use whatever escaping function your db requires this is very important. 
$escapedPW = mysql_real_escape_string($_POST['password']); 

# generate a random salt to use for this account 
$salt = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM)); 

$saltedPW = $escapedPW . $salt; 

$hashedPW = hash('sha256', $saltedPW); 

$query = "insert into user (name, password, salt) values ('$escapedName', '$hashedPW', '$salt'); "; 
?> 

謝謝!

+1

'mysql_real_escape_string'是'mysql'擴展的函數,''mysqli_connect'是'mysqli'擴展的函數。而且你不應該清理用戶的密碼**在使用前,你需要對它進行哈希處理,但在將哈希插入數據庫之前 – zerkms

+0

對於拋出的錯誤消息,有哪些不清楚的地方? – PeeHaa

+0

@PeeHaa:它實際上有點棘手,尤其是對於一個新手 – zerkms

回答

5

您正在使用mysqli擴展來創建數據庫連接。

然後,您正在使用一個完全不同的名爲「mysql」的擴展名(注意最後缺少「i」)來轉義。

轉義需要一個現有的mysql連接。如果不存在,它將使用默認值隱式創建。這樣做失敗。

但它並沒有指出你正確的方向:你必須使用你創建連接的擴展名中的轉義函數。這是mysqli_real_escape_string(注意「mysql」後面加了「i」)。

一般免責聲明:

當使用mysqli的,用準備好的發言擺脫逃避串。

不要使用快速散列函數來散列密碼。包含這個實現用於密碼散列的PHP5.5函數的庫:https://github.com/ircmaxell/password_compat

+1

如果你提到了準備好的陳述 – zerkms

+0

@zerkms就好了:剛剛做到了。 – Sven

相關問題