Reputation: 54949
I am trying to sanatize the input using mysql-real-escape-string before i save the data into my database using CakePHP. And i get the following error
mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'nobody'@'localhost' (using password: NO)
My Code:
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')));
}
}
Upvotes: 2
Views: 2165
Reputation: 2438
In case of complex queries manually constructed Sanitize::escape()
for CakePHP 2.x or for CakePHP 3.x
$connection = ConnectionManager::get('default');
$clean_string = $connection->quote('dirty"string--%/\');
Upvotes: 2
Reputation: 558
mysql_real_escape_string() requires a connection to a database as the second parameter unless you've already opened a connection: http://php.net/manual/en/function.mysql-real-escape-string.php
You can try one the following:
Generally I would recommend using PDO as it's better OO, and is more supported than the mysql_* functions
Upvotes: 1
Reputation: 173
I guess you haven't connected to the mysql db. Try mysql_connect() and mysql_select_db() with the correct credentials
Upvotes: 2