dsgdf45654 2016-11-25 11:02
浏览 53
已采纳

在Wordpress Php文件中使用MySQL UPDATE语句

I have 1 field to update in Mysql database

table -> wp_usermeta

field -> meta_key: _ywpar_user_total_points

field -> meta_value: 4000

The update statement: UPDATE

wp_usermeta` SET `meta_value`= 15 WHERE `user_id`= 1 and `meta_key`="_ywpar_user_total_points"

I've already tried a bunch of code from Wordpress codex and Youtube and others website, like:

global $wpdb;
$wpdb->query
("
    UPDATE $wpdb->wp_usermeta
    SET meta_value = 7000
    WHERE user_id = 1
        AND meta_key = '_ywpar_user_total_points'
");

global $wpdb;
$wpdb->update( 
    'wp_usermeta', 
    array( 
        'meta_value' => 4000,
    ),
    array( 
        'meta_key' => '_ywpar_user_total_points', 'user_id' => 1
    ), 
    array( 
        '%s',   // value1
        '%d'    // value2
    ), 
    array( '%d' ) 
);

none of them are working

  • 写回答

3条回答 默认 最新

  • dpp10181 2016-11-28 04:54
    关注

    @user6582353 Here https://codex.wordpress.org/Class_Reference/wpdb is the ref.and syntax about how update work. see its syntax and understand it

    $wpdb->update( 
        'table', 
        array( 
            'column1' => 'value1',  // string
            'column2' => 'value2'   // integer (number) 
        ), 
        array( 'ID' => 1 ), 
        array( 
            '%s',   // value1
            '%d'    // value2
        ), 
        array( '%d' ) 
    );
    

    Here %s is used for column1 value format which we are passing as string so provided %s and second column2 will be an integer type data so here taken %d.same way for where format too, in where passed only ID of integer type so provided %d only as last argument in array

    Consider your case now

    global $wpdb;
    $wpdb->update( 
        'wp_usermeta', 
        array( 
            'meta_value' => 4000,
        ),
        array( 
            'meta_key' => '_ywpar_user_total_points', 'user_id' => 1
        ), 
        array( 
            '%s',   // value1
            '%d'    // value2
        ), 
        array( '%d' ) 
    );
    

    So here you are changing only 1 value which is integer and the second is where format where you are taking 2 arguments 1 is of string and second is of integer so the final syntax would be something like

     global $wpdb;
        $wpdb->update( 
            'wp_usermeta', 
            array( 
                'meta_value' => 4000,
            ),
            array( 
                'meta_key' => '_ywpar_user_total_points', 'user_id' => 1
            ), 
            array( 
                '%d'     // for meta_value
            ), 
            array( 
                '%s',   // for meta_key
                '%d'    // for user_id
            ) 
        );
    

    Hope its clear to you now and works for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器