Forum Moderators: coopster

Message Too Old, No Replies

DreamWeaver PHP & MySQL Insert & Update Functions UTF-8 Encoding Prob.

PHP & MySQL UTF-8 Encoding Intergration

         

Sherif

11:09 pm on Jan 9, 2010 (gmt 0)

10+ Year Member



Hey Guys,

I am a novice programmer here, and i am learning programing by myself.
I am currently having problems to encode the insert and update text from a form to a MySQL database.

The characters that i am using requires UTF-8 or else, they appear as "? ? ? ?" and so on. At first, i had problems displaying them on the page, then i added these 2 lines of code:

mysql_query("SET NAMES 'utf8'");
mysql_query('SET CHARACTER SET utf8');

and they seemed to work fine.

Now i have a problem when i am updating or inserting data in the table using a php form.
It seems that php inserts/updates the table using the ISO standards, when i need it to use the UTF-8 encoding method.

Below is the code that i am using with the help of dreamweaver to update the table.


$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
$updateSQL = sprintf("UPDATE gis SET Country=%s, Category=%s, `Company Name (AR)`=%s, `Company Name (EN)`=%s, Type=%s, `Address (AR)`=%s, `Address (EN)`=%s, `Phone Number`=%s, `Mobile Number`=%s, Longitude=%s, Latitude=%s, Accuracy=%s WHERE `No`=%s",
GetSQLValueString($_POST['Country'], "text"),
GetSQLValueString($_POST['Category'], "text"),
GetSQLValueString($_POST['Company_Name_AR'], "text"),
GetSQLValueString($_POST['Company_Name_EN'], "text"),
GetSQLValueString($_POST['Type'], "text"),
GetSQLValueString($_POST['Address_AR'], "text"),
GetSQLValueString($_POST['Address_EN'], "text"),
GetSQLValueString($_POST['Phone_Number'], "text"),
GetSQLValueString($_POST['Mobile_Number'], "text"),
GetSQLValueString($_POST['Longitude'], "text"),
GetSQLValueString($_POST['Latitude'], "text"),
GetSQLValueString($_POST['Accuracy'], "text"),
GetSQLValueString($_POST['No'], "int"));

mysql_select_db($database_database, $DatabaseConnection);
$Result1 = mysql_query($updateSQL, $DatabaseConnection) or die(mysql_error());

$updateGoTo = "success.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}

Can anyone just tell me how to make this update code update the database using the utf-8 encoding method?

Thank you so much for your support.
Hope to hearing from you soon.

Sincerely,
Sherif

Sherif

12:49 am on Jan 16, 2010 (gmt 0)

10+ Year Member



I have found the solution....

there are 2 types of code that i had to use to allow php and mysql to use the utf8 encoding method.

/*Adding Code To Encode All Inputs from PHP Form*/
mysql_query("set names 'utf8'");
mb_internal_encoding("UTF-8");

I found that these 2 lines of code, allow the php & mysql to encode the text as utf-8 and allows both compatibilities. Use these 2 lines of code for the form insert update and so on.

on the other hand, if you have or want to show mysql utf-8 fields, you have to use the following:

mysql_query('SET CHARACTER SET utf8');

this should be used directly before the mysql query when extracting data.