2

I inserted foreign lang into mysql db through html form and the result is as below.

 యేసౠకà±à°°à±€à°¸à±à°¤à± జననమౠ

When i insert data by the below statement it works very fine.

$conn->set_charset('utf8');    
$sql = INSERT INTO table_name (column1, column2) VALUES (value1,value2);

Im facing problem when i use a html form to insert data.

Here is the code i use.

<?php

$db_database = 'xxxxx';
$db_hostname = 'xxxx';
$db_username = 'xxxxx';
$db_password = 'xxxxx';

$db_server = mysql_connect($db_hostname, $db_username, $db_password);

mysql_select_db($db_database)
    or die("Unable to connect to database: " . mysql_error());

$title =$_POST['title'];
$lang =$_POST['lang'];

mysql_query ("set character_set_results='utf8'"); 

$sql="INSERT INTO lyrics_a (title, lang)VALUES('$title', '$lang')";
$result=mysql_query($sql);

if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}

else {
echo "ERROR";
}
?>

Html form:

<form method="post" action="/insertdata.php">
<label>TITLE</label>
<input name="title" type="text" id="title" class="form-control">

<label>LANGUAGE</label>
<input name="lang" type="text" id="lang" class="form-control">

<button type="submit" name="submit" id="submit" value="Update">Submit</button>

</form>

Worked for me. Hope this help others.

I made changes to my code as below:

Include below lines in mysql statements.
mysql_connect();
mysql_query("SET NAMES 'utf8'");

Change <form> as below.
<form method="post" action="/insertmysql.php"  enctype="multipart/form-data" accept-charset="UTF-8">

Add the below line at the top of the forms PHP page.
header("Content-Type: text/html;charset=UTF-8");
dan
  • 89
  • 1
  • 1
  • 15

1 Answers1

0

Declare UTF-8 as default charset at the beginning of your PHP page:

<?php
  header('Content-Type: application/json;charset=utf-8');
?>

Also go through http://php.net/manual/en/book.mbstring.php thoroughly to read about Multibyte String (that is meant to what you are looking for).

Rehmat
  • 4,681
  • 3
  • 22
  • 38