OK, here is the situation: I have a table with the fields "ID" (int, auto incremented), "query" (text), and "counter" (int). I am trying to make it so if a selected value exists in the "query" column, the counter of that row should be increased by one. If it doesn't, then a new row should be created with the counter's value being 1. I am using this code:
$all_search_queries = $wpdb->get_results('SELECT * FROM `table`', ARRAY_A);
$found_query = false;
foreach($all_search_queries as $val){
if($_GET['s'] == $val['query']){
$wpdb->update('table', array('query'=>$_GET['s'], 'counter'=>$val['counter']+1), array('ID' => $val['ID']), array('%s', '%d'), array('%d'));
$found_query = true;
break;
}
}
if(!$found_query){
$wpdb->insert('table', array('query'=>$_GET['s'], 'counter'=>1), array('%s', '%d'));
}
Now what it does is it takes $val['counter']
's value and increases it by 2. If I type $val['counter']+2
then it increases it by 4. If I delete the +1
then the value remains unchanged.
Also on that last line where it inserts a new row - 'counter'=>1
actually makes the counter equal to 2. And 'counter'=>15
makes it equal to 16. What gives? This makes no sense to me. Some explanation would be really appreciated. Thanks for your time.