如何在自定义表格中为wordpress创建获取结果和更新表单

时间:2016-06-30 10:10:46

标签: wordpress

<div class="row">
        <?php
        global $wpdb;   
        $rows = $wpdb->get_results( "SELECT id, firstname, lastname, email, phone, voucher FROM wp_offer_user", ARRAY_A);
        foreach ( $rows as $row ) {
            $rowid=$row[id];
            if($row[voucher] == null) { print_r ($row[id]); ?>          
                <div class="col-sm-12 tabel_voucher clearfix">
            <div class="col-sm-2 voucher_box clearfix">
                <div class="col-sm-12 col-xs-6 voucher_data_head">
                    <span class="voucher_head">First Name</span>
                </div>
                <div class="col-sm-12 col-xs-6 voucher_data">
                    <?php echo $row[firstname] ?>
                </div>
            </div>
            <div class="col-sm-2 clearfix">
                <div class="col-sm-12 col-xs-6 voucher_data_head">
                    <span class="voucher_head">Last Name</span>
                </div>
                <div class="col-sm-12 col-xs-6 voucher_data">
                    <?php echo $row[lastname] ?>
                </div>
            </div>
            <div class="col-sm-2 clearfix">
                <div class="col-sm-12 col-xs-6 voucher_data_head">
                    <span class="voucher_head">Mail ID</span>
                </div>
                <div class="col-sm-12 col-xs-6 voucher_data">
                    <?php echo $row[email] ?>
                </div>
            </div>
            <div class="col-sm-2 clearfix">
                <div class="col-sm-12 col-xs-6 voucher_data_head">
                    <span class="voucher_head">Mobile No</span>
                </div>
                <div class="col-sm-12 col-xs-6 voucher_data">
                    <?php echo $row[phone] ?>
                </div>
            </div>
            <div class="col-sm-2 clearfix">
                <div class="col-sm-12 col-xs-6 voucher_data_head">
                    <span class="voucher_head">Register Date</span>
                </div>
                <div class="col-sm-12 col-xs-6 voucher_data">
                    10/01/1991
                </div>
            </div>
            <div class="col-sm-2 clearfix">
                <div class="col-sm-12 col-xs-6 voucher_data_head">
                    <span class="voucher_head">Voucher</span>
                </div>
                <div class="col-sm-12 col-xs-6 voucher_data">               
                    <form id="form-voucher">
                        <div class="field-wrap">                        
                            <input type="text" name="voucher" class="offer-voucher" />
                        </div> 
                        <button name="submit" class="offer-submit">Submit</button>
                        <div class="ajax-loader"></div>
                        <div class="login-error"></div>
                    </form>         
                </div>
            </div>
        </div>
        <?php           
            }   
         }
        ?>
    </div>

这是我的function.php。 //用户个人资料更新

function user_voucher_form() {    
    // The $_REQUEST contains all the data sent via ajax
    if ( isset($_REQUEST) ) {        
        $voucher = $_REQUEST['voucher']; 
        $id = $_REQUEST['id']; 
        global $wpdb;
        $wpdb->update( 
            'wp_offer_user', 
            array( 
            'voucher' => $voucher   // string           
            ), 
            array( 'id' => $id ), 
            array( 
            '%s'    // value1           
            ), 
            array( '%d' ) 
            );
        if ( is_wp_error( $id ) ) {
            echo "Error";
        }
        die();
    }
}
add_action( 'wp_ajax_user_voucher_form', 'user_voucher_form' );
add_action( 'wp_ajax_nopriv_user_voucher_form', 'user_voucher_form' );

我已更新用户个人资料表格。但值不存储在db ...哪个问题.....我使用WordPress wpdb。 如何创建获取结果和更新表...请帮助我

1 个答案:

答案 0 :(得分:0)

您的get_results查询看起来不正确。当你var_dump($rows);时,$行中有什么?

尝试以这种方式格式化(see more query structures here)

$variable = $wpdb->get_results(
    $wpdb->prepare('
        SELECT `column3`
        FROM `wp_table`
        WHERE `column1` = %s
        AND `column2` = %d
        ',
        $var1,
        123
    ),
    OBJECT
);

此外,您的更新结构看起来是正确的 - 但请确认$voucher确实是要更新的数据,$id是where语句:

$wpdb->update(
    wp_table,
        array(
            'column3' => $variable, // the column to update
        ),
        array(
            'column1' => 123, // the first WHERE argument
            'column2' => 'value2', // additional WHERE argument!
        ),
        array('%s'), // the format of the update value
        array(
            '%d', // the format of the first WHERE argument
            '%s' // the format of the second WHERE argument
        )
    );