douqiang6448 2016-10-06 19:48
浏览 12

使用PHP中的wpdb插入数据库

I try to make a function to insert more fields in database. I find an example, but maybe more parameters is deprecated...or i have an error.

Here is my code:

private function _setUserPersonalDetails($blog_id, $personal_data, $operation = 'insert') {
    global $wpdb;

    // Strip any html tags from the about and address fields.
    $tmp = array();
    foreach ($personal_data as $key => $val) {
      if (($key === 'about') || ($key === 'address')) {
        $tmp[$key] = strip_tags($val);
      }
      else {
        $tmp[$key] = $val;
      }
    }
    $personal_data = $tmp;

    if ($operation == 'insert') {
      // Save personal data
      $encoded_data = json_encode($personal_data);
      $type = 'personal_data';
      $q = $wpdb->prepare('INSERT INTO wp0_users_conta_data (blog_id, type, data) VALUES (%d, %s, %s)', $blog_id, $type, $encoded_data);
      $wpdb->query($q);
    }

    if ($operation == 'update') {
      // Update personal data
      $encoded_data = json_encode($personal_data);
      $type = 'personal_data';
      $q = $wpdb->prepare('UPDATE wp0_users_conta_data set data=%s where blog_id=%d and type=%s', $encoded_data, $blog_id, $type);
      $wpdb->query($q);
    }
  }
}

I spent more one week to solve the problem, but i don't find any solutions. I appreciate any helps. Thank you!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 mmocr的训练错误,结果全为0
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀