在 PHP 中清理发往数据库的用户输入
我有这个代码:
$query = "select id from votes where username = '$user' and article_id = $this->id";
我尝试使用这个代码来清理它:
$query = sprintf("select id from votes where username = '$user' and article_id = $this->id",
mysql_real_escape_string($user),
mysql_real_escape_string($password));
但是我在 mysql_real_escape 行中收到此错误:
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'mexautos'@'localhost' (using password: NO) in /home/mexautos/public_html/kiubbo/data/article.php on line 145 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/mexautos/public_html/kiubbo/data/article.php on line 145 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'mexautos'@'localhost' (using password: NO) in /home/mexautos/public_html/kiubbo/data/article.php on line 146 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/mexautos/public_html/kiubbo/data/article.php on line 146
我在这里获取用户名,我不知道它是否足够安全:
function getUsername(){ return $this->username; }
Thx
I have this code:
$query = "select id from votes where username = '$user' and article_id = $this->id";
I tried this code to sanitize it:
$query = sprintf("select id from votes where username = '$user' and article_id = $this->id",
mysql_real_escape_string($user),
mysql_real_escape_string($password));
but I get this error for the mysql_real_escape lines:
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'mexautos'@'localhost' (using password: NO) in /home/mexautos/public_html/kiubbo/data/article.php on line 145 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/mexautos/public_html/kiubbo/data/article.php on line 145 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'mexautos'@'localhost' (using password: NO) in /home/mexautos/public_html/kiubbo/data/article.php on line 146 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/mexautos/public_html/kiubbo/data/article.php on line 146
I get the user name here, I dont know if its safe enough:
function getUsername(){ return $this->username; }
Thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我建议使用成熟的数据库抽象层,例如 Zend_Db (有那里有很多)。 对于生产系统,我不建议实施您自己的自制解决方案。
I'd recommend using a mature DB abstraction layer like Zend_Db (there are tons of them out there). Implementing your own homebrew solution is not something I'd recommend for a production system.
就像其他人所说,不是“$user”而是“%s”,并且您需要一个开放的连接。
@托马拉克
sprintf 更快 - 这就是使用它的原因 - 它是一个本机 C 函数。
Like the other said, not '$user' but '%s' and you need an open connection.
@Tomalak
sprintf is faster - that's the reason why to use it - it is a native C function.
您需要先建立 mysql 连接,然后才能使用 mysql_real_escape_string。
You need a mysql connection before you can use mysql_real_escape_string.
我建议使用 准备好的语句 代替
sprintf
I would suggest using prepared statements for this instead of
sprintf
不确定这是否是导致您问题的原因,但我相信 sprintf 语句中的变量不应该是 '$user' 和 '$this->id',而应该是 '%s'
https://www.php.net/sprintf
Not sure if this is what's causing your problem, but I believe the variables in your sprintf statement shouldn't be '$user' and '$this->id', but they should be '%s'
https://www.php.net/sprintf
您检查链接了吗? 是否活跃? 使用前需要先连接 mysql_real_escape_string()< /a>
你是不是忘记设置密码了?
尝试:(
如果没有密码,请输入 Enter)
另外,检查您的 sprintf() 函数,您需要使用 %s 绑定变量
Did you check the link ? Is it active ? You need to be connected before to use mysql_real_escape_string()
Don't you forget to set the password ?
Try:
(type Enter if no password)
Also, check out your sprintf() function, you need to use the %s to bind your variable
您需要一个连接才能使用 mysql_real_escape_string(),因为它使用服务器的编码类型来帮助清理。
另外 sprintf() 应该看起来像这样
You need a connection to use mysql_real_escape_string() because it uses the server's encoding type to help santitize.
Also the sprintf() should look something like this