Professional CodeIgniter, Thomas Myer
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);
}
c09.indd 275
c09.indd 275
6/10/08 5:38:00 PM
6/10/08 5:38:00 PM