更新数据库后获取`Warning:mysqli_real_escape_string()`

时间:2016-12-13 04:30:59

标签: php wordpress mysqli

我尝试将自定义代码更新为自定义数据库表 在我发送数据后,我得到了:

  

警告:mysqli_real_escape_string()期望参数2为字符串,   第1156行中的... / wp-includes / wp-db.php中给出的数组

我的表结构是:
表名:wp_wlm_user_options
列名:IDuser_idoption_nameoption_value

我的代码是:

if (isset($_POST['submit'])) {
    $user_id = $_POST['user_id'];
    $table = 'wp_wlm_user_options';

    $meta = array();
    foreach ($wlm_user_info as $key => $value) {
        $get_meta = $value['option_name'];
        array_push($meta, $get_meta);
    }

    //var_dump($meta);

    $value = array(
         'custom_firstname' => $_POST['first_name'],
         'custom_lastname' => $_POST['last_name'],
         'custom_text_dateofbirth' => $_POST['text_dateofbirth'],
         'custom_radio_gender' => $_POST['radio_gender'],
         'custom_landlinephone' => $_POST['landlinephone'],
         'custom_GoogleHangoutsId' => $_POST['GoogleHangoutsId']
    );
    $format = array(
         '%s',
         '%s'
    );

    //print_r($meta,$value);

    $data = array(
        'user_id' => $user_id,
        'option_name' => $meta,
        'option_value' => maybe_serialize($value)
    );

    $format = array('%d','%s','%s');

    $where = array(
        'user_id' => $user_id,
        'option_name' => $meta
    );

    $x = $wpdb->update($table, $data, $where, $format);
    //$x = $wpdb->update($table, $data, $where);
    if($x){
        echo '<h1>data has been save</h1>' ;
    }

有谁能告诉我这有什么问题?

1 个答案:

答案 0 :(得分:0)

由于您的信息不佳,我们只能猜测。

你可能想要这样的东西:

if (isset($_POST['submit']))
{
  $user_id = $_POST['user_id'];
  $table = 'wp_wlm_user_options';
  $values = array(
    'custom_firstname'        => $_POST['first_name'      ],
    'custom_lastname'         => $_POST['last_name'       ],
    'custom_text_dateofbirth' => $_POST['text_dateofbirth'],
    'custom_radio_gender'     => $_POST['radio_gender'    ],
    'custom_landlinephone'    => $_POST['landlinephone'   ],
    'custom_GoogleHangoutsId' => $_POST['GoogleHangoutsId']
  );

  $format      = array('%s');
  $whereformat = array('%d','%s');

  foreach($values as $key => $val)
  {
    $data = array(
      'option_value' => maybe_serialize($val)
    );

    $where = array(
      'user_id' => $user_id,
      'option_name' => $key
    );

    $num_rows = $wpdb->update($table, $data, $where, $format, $whereformat);
    if($num_rows !== false){
      echo "<h1>$key has been saved</h1>" ;
    }
  }
};

上面的代码会针对每个option_name调用执行多次更新,将数据映射到option_valueupdate()。您不需要使用未更改的值更新WHERE子句的字段。

您可以在wordpress.org

上找到官方文档