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条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装