Professional CodeIgniter

(singke) #1

Chapter 9: Security and Performance


275


Securing the MColors Model


MColors is a very simple model. The only changes you ’ ll need to make are to the getColor() function
and the functions associated with adding, updating, and deleting content, which are:

addColor()

updateColor()

deleteColor()

getColor()

Here is the code:

function getColor($id){
$data = array();
$options = array(‘id’ => id_clean($id) );
$Q = $this-> db-> getwhere(‘colors’,$options,1);
if ($Q- num_rows() > 0){
$data = $Q- >row_array();
}
$Q-> free_result();
return $data;
}

function createColor(){
$data = array(
‘name’ => db_clean($_POST[‘name’],32) ,
‘status’ => db_clean($_POST[‘status’],8)
);
$this-> db-> insert(‘colors’, $data);
}

function updateColor(){
$data = array(
‘name’ => db_clean($_POST[‘name’],32) ,
‘status’ = > db_clean($_POST[‘status’],8)

);

$this-> db-> where(‘id’, id_clean($_POST[‘id’]) );
$this-> db-> update(‘colors’, $data);

}

function deleteColor($id){
$data = array(‘status’ => ‘inactive’);
$this-> db-> where(‘id’, id_clean($id) );
$this-> db-> update(‘colors’, $data);
}




Free download pdf