$FieldNamesValuesArray = array();
      $FieldNamesValuesArray['ColumnName1'] = $ColumnName1;
      $FieldNamesValuesArray['ColumnName2'] = $ColumnName2;
      db_user_write_fields($user_id, $FieldNamesValuesArray);
function db_user_write_fields($user_id, $FieldNameValueArray)
  global $wpdb;
  $sql = "UPDATE tbl_my_table SET ";

  $Values = array();
  $Types = "";
  $FirstField = True;
  foreach ($FieldNameValueArray as $key => $value)
    if ($FirstField)
      $sql .= " $key = ? ";
      $sql .= ", $key = ? ";
    $FirstField = False;
    $Values[] = $value;
    $Types .= "s";    //Mark all types a string as MySql will happily accept numeric values as a string also
  $sql .= " WHERE user_id = ? ";
  $Values[] = $user_id;
  $Types .= "s"; 

  $stmt = $maindb->prepare("$sql");
  $stmt->bind_param($Types, ...$Values);      //"..." is an argument unpacking operator, its converts the array into individual variables required by bind_param()
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.


Your email address will not be published.