gpt4 book ai didi

php - MySQL 选择,输出然后删除

转载 作者:行者123 更新时间:2023-11-28 23:39:52 25 4
gpt4 key购买 nike

我在 WP 安装中使用此查询从自定义表中输出最低代码。它成功地输出了 ID 最低且取值为“0”的行。所以,这一切都很好——但是,在同一个函数中,我希望输出的代码将其设置为 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 );

最佳答案

您需要使用 wpdb->update() :

$wpdb->update('vouchers', array('taken' => 1), array('code' => $RetrievedVoucherCode));

关于php - MySQL 选择,输出然后删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34712518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com