WordPress: $wpdb->get_results(); returns 第一次循环后结果错误
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://whosebug.com/help/mcve
我目前正在 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://whosebug.com/help/mcve