phpmysqlwordpressgravity-forms-plugin

WordPress: $wpdb->get_results(); returns wrong results after the first loop


I am currently working on a plugin in Wordpress (latest version). Now everything went just fine except this one thing when hooking into GravityForms.

Here is my code:

function DbUpdateServices($services){
$choices = $services->choices;
print_r($choices);
global $wpdb;
$allValues = array();
foreach($choices as $choice){
$text = $choice["text"];
$value = $choice["value"];
$allValues[] = $value;
$name_exists = $wpdb->get_results("SELECT * FROM quotation_diensten WHERE dienstNaam='$text'", ARRAY_A);
echo("SELECT * FROM quotation_diensten WHERE dienstNaam='".$text."'");

if($wpdb->num_rows == 0){
  $value_exists = $wpdb->get_results("SELECT * FROM quotation_diensten WHERE dienstValue = $value", ARRAY_A);
  if($wpdb->num_rows == 0){
    echo "No rows found";
    $wpdb->insert('quotation_diensten',
      array(
        "dienstNaam" => $text,
        "dienstValue" => $value
      ),
      array('%s','%d')
    );
  } else {
    echo "Row found";
    $wpdb->update("quotation_diensten",
      array(
        "dienstNaam" => $text
      ),
      array( "dienstValue"=> $value
      ),
      array("%s")
    );
  }
} else {
  echo "($value,$text) Bestaat,<br>";
  $wpdb->update("quotation_diensten",
    array(
      "dienstValue" => $value
    ),
    array( "dienstNaam"=> $text
    ),
    array("%d")
  );
}
$wpdb->flush();
echo "<hr>";
//delete
$allServices = $wpdb->get_results("SELECT * FROM quotation_diensten", ARRAY_A);
foreach ($allServices as $service) {
  if(!in_array($service["dienstValue"], $allValues)){
      //verwijderen
      $wpdb->delete( "quotation_dienstaanvraag", array('dienstenID'=>$service["dienstenID"]) );
      $wpdb->delete( "quotation_bedrijfsdiensten", array('dienstenID'=>$service["dienstenID"]) );
      $wpdb->delete( "quotation_diensten", array('dienstenID'=>$service["dienstenID"]) );
  }
}
}
}

This is the content of $choices:

array(3) {
   [0]=>
      array(3) {
         ["text"]=>
            string(9) "Service 1"
         ["value"]=>
            string(1) "1"
         ["isSelected"]=>
            bool(false)
      }
       [1]=>
          array(4) {
             ["text"]=>
                string(9) "Service 2"
             ["value"]=>
                string(1) "2"
             ["isSelected"]=>
                bool(false)
             ["price"]=>
                string(0) ""
           }
       [2]=>
           array(4) {
               ["text"]=>
                   string(9) "Service 3"
               ["value"]=>
                   string(1) "3"
               ["isSelected"]=>
                   bool(false)
               ["price"]=>
                   string(0) ""
             }
          }

The first time the foreach loop gives me a row back which is correct.

The second and third time it says 0 rows.

This is my database structure:

dienstenID      | dienstNaam | dienstValue
221                Service 1 |    1
351             |  Service 2 |    2
352             |  Service 3 |    3

Me and my colleague couldn't figure out why. What is wrong here?


Solution

  • First of all the following line is definitely wrong:

    $name_exists = $wpdb->get_results("SELECT * FROM quotation_diensten WHERE dienstNaam='$text'", ARRAY_A);
    

    Why? Because a variable name within single quotes is not interpreted, you are passing the string value $text to the database which is certainly not what you want. I guess if you change this, it will solve your problem.

    Change it by using $wpdb->prepare for your code. It is bad practice to write variables into the SQL yourself.