嗨,由于某种原因,我不知道我一直收到以下错误:
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'); ";
?>谢谢!
发布于 2014-02-10 00:43:19
您正在使用mysqli扩展来创建数据库连接。
然后使用一个完全不同的名为"mysql“的扩展(请注意末尾缺少的"i”)来转义。
转义需要一个现有的mysql连接。如果不存在,则将使用默认值隐式创建它。这样做是失败的。
但是它并没有指出正确的方向:您必须使用创建连接的扩展中的转义函数。它是mysqli_real_escape_string (注意在“mysql”之后添加了"i“)。
一般免责声明:
当使用MySQLI时,使用准备好的语句来摆脱转义字符串。
不要使用快速哈希函数来散列密码。包括实现用于密码散列的PHP5.5函数的库:compat
https://stackoverflow.com/questions/21667233
复制相似问题