function InsertUpdate_v2($mysqli,$table_name,$i_data,$u_data){   

  $dt=array();

  $i_fields = array_keys($i_data);  

  $i_values = array_map(array($mysqli, 'real_escape_string'), array_values($i_data));



  $u_fields = array_keys($u_data);  

  $u_values = array_map(array($mysqli, 'real_escape_string'), array_values($u_data));



  $sql = "INSERT INTO `$table_name` (`".implode("`,`",$i_fields)."`) VALUES ('".implode("','", $i_values)."')";



  // start the actual SQL statement

  $sql .= " on duplicate key UPDATE ";



  // loop and build the column /

  $sets = array();

  foreach($u_data as $column => $value) {

       $sets[] = "`".$column."`='".$value."'";

  }

  $sql .= implode(', ', $sets);

  $dt['sql']=$sql.";";

  // run and return the query result

  // return mysqli_query($mysqli,$sql);

  if(mysqli_query($mysqli,$sql)) {

    $dt['result']="Success";

  } else {

    $dt['result']="Error";    

  }  

  return json_encode($dt);

}