在使用CakePHP将数据保存到数据库之前,我正在尝试使用mysql--真正的转义-字符串对输入进行清理。我得到以下错误
mysql_real_escape_string() Function.mysql-真正的-转义-字符串:拒绝用户‘任何人’@‘localhost’的访问(使用密码: NO)
我的守则:
public function admin_videos($id = null) {
if(!($this->isLogged() && $this->isAuthorized())) {
$this->redirect(array('controller' => 'users', 'action' => 'login', 'admin' => true));
}
if ($this->request->is('post')) {
$this->request->data['MovieVideo']['video'] = mysql_real_escape_string($this->request->data['MovieVideo']['video']);
$this->request->data['MovieTrailer']['video'] = mysql_real_escape_string($this->request->data['MovieTrailer']['video']);
if ($this->Movie->saveAll($this->request->data)) {
$this->Session->setFlash('The movie has been saved', 'admin/flash_success');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('The movie could not be saved. Please, try again.', 'admin/flash_error');
}
} else {
$this->request->data = $this->Movie->find('first', array('conditions' => array('Movie.id' => $id), 'contain' => array('MovieTrailer', 'MovieVideo')));
}
}发布于 2012-05-24 07:01:04
来自文档
如果使用CakePHP的ORM方法(如find()和save())和适当的数组表示法(即,),CakePHP已经保护您不受SQL注入的影响。数组(‘field’=> $value),而不是原始SQL。
因此,忘记手动调用mysql_real_escape_string()吧。
发布于 2012-05-24 07:01:16
我想你还没有连接到mysql数据库。使用正确的凭据尝试mysql_connect()和mysql_select_db()
发布于 2012-05-24 07:42:11
mysql_real_escape_string()需要将到数据库的连接作为第二个参数,除非您已经打开连接:http://php.net/manual/en/function.mysql-real-escape-string.php
您可以尝试以下一个:
通常,我建议使用PDO,因为它比mysql_*函数更好,更受支持
https://stackoverflow.com/questions/10732600
复制相似问题