samedi 10 janvier 2015

Why PHP if..esleif..else starts from beginning again after MySQL query in it

I am using following code to run update query if value is not duplicate in database.



if($lastvote==false){ // There isn't duplicate entry, just run INSERT query.
$this->save();
return "Vote recorded!";
}elseif($lastvote=="down" && $this->vote_value=="down"){
return "Already voted down!";
}elseif($lastvote=="up" && $this->vote_value=="up"){
return "Already voted up!";
}elseif($lastvote=="up" && $this->vote_value=="down"){
$this->update_last($this->topic_id, $this->updated_by, $this->vote_value);
return "Vote changed to down!";
}elseif($lastvote=="down" && $this->vote_value=="up"){
$this->update_last($this->topic_id, $this->updated_by, $this->vote_value);
return "Vote changed to up!";
}


The queries works well. But after function



$this->update_last()




private function update_last($topic, $by, $value){
global $database;
$sql = "UPDATE ".static::$table_name." SET vote_value='".$value."' WHERE topic_id = ".$topic." AND updated_by = ".$by;
$database->query($sql);
return ($database->affected_rows() == 1) ? true : false;
}


it is not returning "Vote changed to up/down" as per last two conditions. Rather it returning "Already voted up/down" from first two queries after successfully executing the UPDATE query from last two conditions.


Please guide me where I am making mistake?


Aucun commentaire:

Enregistrer un commentaire