我在WP安装中使用此查询从自定义表输出最低的代码。它成功输出具有最低ID的行,并取消了' 0'。所以,一切都很好 - 但是,在同一个函数中,我希望输出的代码然后将SET取为1,而不是0.我已经尝试了很多变化,但不能完全破解它。有任何想法吗?感谢。
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 );
答案 0 :(得分:1)
您想要使用wpdb->update():
$wpdb->update('vouchers', array('taken' => 1), array('code' => $RetrievedVoucherCode));
答案 1 :(得分:0)
如果代码是唯一的
UPDATE vouchers SET taken=1 WHERE code = (SELECT MIN(code) FROM vouchers WHERE taken=0)
答案 2 :(得分:0)
您可以尝试组合UPDATE和SELECT语句,类似于explained here。