The task is this: the application accepts user input (an event is created) and writes them to an external database using php scripts on the server, then displays this data. Accepted in Russian. If I create an event in the application, then everything is fine in it, but in the debris database. If I create in a DB, then in a DB everything is good, and in the appendix question marks. It seems everywhere everything is in utf-8

form:

@Override protected String doInBackground(Void... v) { HashMap<String,String> params = new HashMap<>(); params.put(Config.EVENTS_COLUMN_TITLE, title); params.put(Config.EVENTS_COLUMN_DATE, date); params.put(Config.EVENTS_COLUMN_TIME, time); params.put(Config.EVENTS_COLUMN_ADDRESS, address); params.put(Config.EVENTS_COLUMN_PHONE, phone); params.put(Config.EVENTS_COLUMN_DESCRIPTION, description); RequestHandler rh = new RequestHandler(); String result = rh.sendPostRequest(Config.URL_ADD, params); return result; } 

connect:

 public String sendPostRequest(String requestURL, HashMap<String, String> postDataParams) { StringBuilder sb = new StringBuilder(); try { URL url = new URL(requestURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); OutputStream os = conn.getOutputStream(); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, "UTF-8")); writer.write(getPostDataString(postDataParams)); writer.flush(); writer.close(); os.close(); int responseCode = conn.getResponseCode(); if (responseCode == HttpsURLConnection.HTTP_OK) { BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); sb = new StringBuilder(); String response; while ((response = br.readLine()) != null){ sb.append(response); } } } catch (Exception e) { e.printStackTrace(); } return sb.toString(); } 

php:

 <?php if($_SERVER['REQUEST_METHOD']=='POST'){ $title = $_POST['title']; $date = $_POST['date']; $time = $_POST['time']; $address = $_POST['address']; $phone = $_POST['phone']; $description = $_POST['description']; if($title == '' || $date == '' || $time == '' || $address == '' || $phone == '' || $description == ''){ echo 'Заполните все поля, пожалуйста'; }else{ require_once('db_connect.php'); $sql = "INSERT INTO events_tab (title,date,time,address,phone,description) VALUES ('$title','$date','$time','$address','$phone','$description')"; if(mysqli_query($con,$sql)){ echo 'Событие добавлено'; }else{ echo 'Событие не добавлено'; } mysqli_close($con); } } ?> 

DB encoding (is this it? I don’t confuse it): enter image description here

connection file:

 <?php define('HOST', 'localhost:3306'); // db server define('USER', 'пользователь'); // db user define('PASS', 'пароль'); // db password define('DB', 'базаданных'); // db name $con = mysqli_connect(HOST, USER, PASS, DB) or die('Unable to Connect'); ?> 
  • Most likely it's in php, but rather in how you create a connection to the database there - show this place. Well, show also the settings for the code of the database itself - YuriySPb
  • @ YuriySPb added - FatSlowpoke
  • What about the $con variable? There as the coding is registered? Be careful - do not put out the login password from the database - YuriiSPb
  • @ YuriySPb added, something tells me that it is not spelled out here - FatSlowpoke

1 answer 1

Try to set the encoding of the connection to the database as follows :

 mysqli_set_charset($con, "utf8"); 

Or like this, using pdo :

 $user = "user"; $pass = "pass "; $db = "db"; $host = "host"; $charset = "utf8"; $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $opt = array ( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ); $pdo = new PDO($dsn, $user, $pass, $opt); 
  • one
    Magnifico, thank you very much - FatSlowpoke