繁体   English   中英

WordPress:$ wpdb-> get_results(); 在第一个循环后返回错误的结果

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

我目前正在使用Wordpress(最新版本)中的插件。 现在,除了挂在GravityForms上的这一件事外,一切都很好。

这是我的代码:

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"]) );
  }
}
}
}

这是$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) ""
             }
          }

foreach循环第一次给我返回正确的行。

第二次和第三次显示为0行。

这是我的数据库结构:

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

我和我的同事不知道为什么。 怎么了

首先,以下几行肯定是错误的:

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

为什么? 因为单引号中的变量名不会被解释,所以您要将字符串值$text传递给数据库,这肯定不是您想要的。 我想如果您更改此设置,它将解决您的问题。

通过使用$wpdb->prepare为您的代码进行更改。 自行将变量写入SQL是一种不好的做法。

第三件事是您已经在此处发布了很多代码的方法-请阅读以下内容: https : //stackoverflow.com/help/mcve

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM