0
現在Wordpress(最新版)のプラグインで作業しています。 GravityFormsにフックする際に、この1つのこと以外はすべてうまくいった。
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ループバック私に行を与えて初めて:
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
の内容は次のとおりです。
は、ここに私のコードです。
2回目と3回目には、0
行と表示されます。
これは私のデータベース構造である:
dienstenID | dienstNaam | dienstValue
221 Service 1 | 1
351 | Service 2 | 2
352 | Service 3 | 3
私と私の同僚は、なぜ把握できませんでした。ここで何が間違っていますか?
'$ name_exists = $ wpdb->準備( "quotation_diensten SELECT * FROM WHERE dienstNaamは= '%s' は"、$テキストを); ' は2番目の要求でまだ0行を返します。 – Emiel
一重引用符を削除する必要があります。 – Blackbam
'$ name_exists = $ wpdb-> prepare(" SELECT * FROM quotation_diensten WHERE dienstNaam =%s "、$ text); ' – Blackbam