我在WP安裝中使用此查詢來輸出自定義表中最低的代碼。它成功輸出ID最低的行,並且採用'0'。所以,這一切都很好 - 但是,在同一個函數中,我希望輸出的代碼將它的SET設置爲1,而不是0.我已經嘗試了很多變體,但無法完全破解它。有任何想法嗎?謝謝。MySQL選擇,然後輸出OUTPUT
function voucher_func() {
global $wpdb;
$vouchers = $wpdb->get_results("SELECT MIN(code) AS code FROM vouchers WHERE taken=0");
foreach($vouchers as $voucher){
return $voucher->code;
}
}
add_shortcode('latest_voucher', 'voucher_func');
從第一點建議,我已經得到了這麼多。這仍然輸出正確的數據,但犯規更新「所採取」 1
function voucher_func() {
global $wpdb;
$vouchers = $wpdb->get_results("SELECT MIN(code) AS code FROM vouchers WHERE taken=0");
foreach($vouchers as $voucher){
return $voucher->code;
}
}
$wpdb->update('vouchers', array('taken' => 1), array('code' => $voucher->code));
add_shortcode('latest_voucher', 'voucher_func');
這是我現在有...
function voucher_func() {
global $wpdb;
$vouchers = $wpdb->get_results("SELECT MIN(code) AS code FROM vouchers WHERE taken=0");
foreach($vouchers as $voucher){
$wpdb->update('vouchers', array('taken' => 1), array('code' => $voucher->code));
return $voucher->code;
}
}
add_shortcode('latest_voucher', 'voucher_func');
這是我最後和工作代碼:
function voucher_func() {
global $wpdb;
$vouchers = $wpdb->get_results("SELECT MIN(code) AS code FROM vouchers WHERE taken=0");
foreach($vouchers as $voucher){
return $voucher->code;
}
}
add_shortcode('latest_voucher', 'voucher_func');
function action_wpcf7_mail_sent($contact_form) {
global $wpdb;
$vouchers1 = $wpdb->get_results("SELECT MIN(code) AS code FROM vouchers WHERE taken=0");
foreach($vouchers1 as $voucher1){
$wpdb->update('vouchers', array('taken' => 1), array('code' => $voucher1->code));
}
};
add_action('wpcf7_mail_sent', 'action_wpcf7_mail_sent', 10, 1);
什麼*有*你試過,沒有工作? – jbafford
DELETE FROM voucher(.... SELECT ...)和其他變化:-( – dmt
這是最後的手段,我寧願保留記錄,如果可能,使用SET採取= 1,但我不明白 – dmt