Hi guys, this is my first post here, and i promise ive done a lot of research before deciding to seek help; I really am totally lost now. Basically I have written a plugin that updates a score value in my database.
Very simply, i use an add_action and within the function i call a wpdb->update. I print the value just before and just after it writes to the database and the echo is correct. However when i look in the database with phpmyadmin i see that whatever the SQL i used, it doubles itself. So if I try to increment a column by 1, it does it twice. If i use an insert, it inserts it twice. Even if I put a die straight after it, it still does it <:(
Here is the code exactly as it is:
add_action( "loop_start", "start" );
function start( $post )
{
global $wpdb;
$USER_ACHIEVEMENTS_DB = $wpdb->prefix.PG_USER_ACHIEVEMENTS;
$userAchievement = $wpdb->get_row("SELECT * FROM $USER_ACHIEVEMENTS_DB WHERE achievement = 'Explorer' AND user_id = 1");
$currentScore =(int)$userAchievement->score;
//Print just before
echo "before $currentScore";
$currentScore += 1; //increment
//Call the update
$wpdb->update( "$USER_ACHIEVEMENTS_DB", array( 'stage' => 1, 'level' => $currentScore, 'score' => $currentScore ),
array( 'user_id' => 1, 'achievement' => "Explorer" ),
array( '%d', "%s", '%d', ),
array( '%d', '%s' ));
echo "after $currentScore"; //This is correct. but in the DB its always double what I increment it with.
die("FIN");
}