• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Update and checking

#1
[eluser]VivaUkraine[/eluser]
How to ensure that updating was succesfull?

i've a method:

Code:
function update_news($id){
    $this->db->where('id', $id);
    $this->db->update('news', $_POST);
    
    }

how can i check was the data updated and if no to output some error message?

#2
[eluser]woopsicle[/eluser]
hi,

use $this->db->affected_rows()

i.e.

Code:
if ($this->db->affected_rows() >= 1)
{
    //success
}
else
{
    //error
}

#3
[eluser]marcoss[/eluser]
[quote author="VivaUkraine" date="1182910116"]How to ensure that updating was succesfull?

i've a method:

Code:
function update_news($id){
    $this->db->where('id', $id);
    $this->db->update('news', $_POST);
    
    }

how can i check was the data updated and if no to output some error message?[/quote]

$this->db->update returns true if updated, so just check on that.

Code:
function update_news($id){
    $this->db->where('id', $id);
    $result = (bool) $this->db->update('news', $_POST);
    if($result) :
        // do stuff
    else :
        //error
    endif;
}

btw, don't use raw $_POST to insert data directly into the database, use (at least) CI Input library instead.

#4
[eluser]VivaUkraine[/eluser]
[quote author="marcoss" date="1182941845"]

btw, don't use raw $_POST to insert data directly into the database, use (at least) CI Input library instead.[/quote]

hmmm what do you mean?

#5
[eluser]thunder uk[/eluser]
The $_POST array *could* contain something nasty that screws up your database (or worse).

So, instead, get your POSTed values into variables that are nice and clean and free from nasties.

eg

$title = $this->db->escape($this->input->post('newstitle'));
$body = $this->db->escape($this->input->post('newsbody'));

This still isn't foolproof since some of the bad guys know how to use multibyte character encoding to bypass the above, but it's still a good bit safer than passing raw $_POST data


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.