function InsertUpdate($mysqli,$table_name,$i_data,$u_data,$v=0){   


  $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);





  if($v==1) {


    echo $sql.";<br>";


  }


  return mysqli_query($mysqli,$sql);


}