Welcome Guest, Not a member yet? Register   Sign In
Update until 2 and no more...
#4

[eluser]Alhazred[/eluser]
To debug printing some values I also added an exit; in a not convenient place, now I've removed it, put somewhere else and I think I've found the problem.

My query is inside a while which is inside a transaction, this is the code with the debug lines
Code:
$this->db->trans_begin();
while($inserzione['active_banner'] > 0)
{
    //row to update selection which ends with
    $row = $result->row_array();

    $set = array('banner' => $row['banner']+1); //$row['banner'] contains the old number of banners
    $this->db->where('id',$row['id']);
    $this->db->update($table,$set);
    echo $this->db->last_query()."<br />"; //debug string
    if ( $this->db->affected_rows() == 1)
    {
        $inserzione['active_banner']--;
    }
    else //update error
    {
        $this->db->trans_rollback();
        echo 'Inside the else<br />'; //debug string
        exit; //stop to see the printed strings
        return FALSE;
    }
}
if ($this->db->trans_status() !== FALSE)
{
    $this->db->commit();
    return TRUE;
}
else
{
    $this->db->rollback();
    return FALSE;
}
the problem appears when $inserzione['active_banner'] is > 1 and for the 1st and 2nd iteration is selected the same entry to update.
The output is
UPDATE `prodotti` SET `banner` = 3 WHERE `id` = '1-1344638814-2'
UPDATE `prodotti` SET `banner` = 3 WHERE `id` = '1-1344638814-2'
Inside the else

for both the iteration, the value to set is 3.

I think that happens as follow:
$inserzione['active_banners'] = 2
1st iteration
entry with 1-1344638814-2 id is selected
banner = 2
banner + 1 = 3
UPDATE `prodotti` SET `banner` = 3 WHERE `id` = '1-1344638814-2' is executed and succeeds
$inserzione['active_banners'] = 1
2nd iteration
entry with 1-1344638814-2 id is selected again
banner = 2
banner + 1 = 3
UPDATE `prodotti` SET `banner` = 3 WHERE `id` = '1-1344638814-2' is executed and result_rows is 0 because the banner value is already 3

The problem is that the 2nd iteration reads again the value actually stored into the db, not the updated one from the 1st iteration, so the "new" value is again 3 and not 4.

I think I have to change the logic.


Messages In This Thread
Update until 2 and no more... - by El Forum - 08-11-2012, 10:24 AM
Update until 2 and no more... - by El Forum - 08-11-2012, 10:36 AM
Update until 2 and no more... - by El Forum - 08-11-2012, 11:02 AM
Update until 2 and no more... - by El Forum - 08-11-2012, 12:44 PM



Theme © iAndrew 2016 - Forum software by © MyBB