$wpdb->更新是否已完成保存触发器?

时间:2013-07-26 作者:Nathan Powell

我有一个自定义表,我正在通过wp\\u ajax操作更新它。插入一行并插入除id 这是一个AUTO_INCREMENT. 我有一个ajax响应,它使用新数据在页面上创建新行。在我的响应中,我需要查看是否添加了行,然后获取要显示的记录。我当然可以打印除id 使用我预定义的变量,但我也想获得动态创建的数字!帮助我需要一个if 语句以查看行何时完成保存。

问题在于我的php,特别是$carrier_id:

<?php
add_action(\'wp_ajax_change_carrier\', \'carrier_change_callback\');
function carrier_change_callback() {
    global $wpdb;

    //New Carrier
    if ($_POST[\'newCarrier\']){
        $wpdb->insert( 
            $wpdb->prefix.\'bf_carriers\', 
            array( 
                \'name\' => stripslashes($_POST[\'newName\']), 
                \'label\' => $_POST[\'newLabel\'],
                \'service_center_path\' => $_POST[\'newHtm\'],
                \'image_name\' => $_POST[\'newImage\']
            )
        );
        //set the variables to return the new Table Row
        $carrier_name = stripslashes($_POST[\'newName\']);
        $carrier_image = $_POST[\'newImage\'];
        $carrier_label = $_POST[\'newLabel\'];
        $carrier_htm = $_POST[\'newHtm\'];
        $carrier_id = $wpdb->get_row("SELECT id FROM $wpdb->prefix.\'bf_carriers\' WHERE image_name = $carrier_image");
?>
        <tr>
        <!-- Lots of junk going on in here to display above variables and return the response to my ajax call -->
        </tr>
<?php   
    die();
    }
}

1 个回复
最合适的回答,由SO网友:gmazzap 整理而成

插入后,为AUTO_INCREMENT 可以通过以下方式访问列:

$wpdb->insert_id
最后一句话只是从codex.

结束

相关推荐

检查插件中是否存在带有AJAX的前端电子邮件地址

我正在努力想办法解决这个问题。我在这里遵循了针对AJAX的wordpress codex:http://codex.wordpress.org/AJAX_in_Plugins我一直返回的是0,我从来没有得到1。知道为什么吗?我的代码如下。PHP:add_action(\'wp_enqueue_scripts\', \'live_validation\' ); add_action(\'wp_ajax_validate_email\', \'validate_email_input\');