Forum Moderators: coopster
Server: Apache 2.0.53
MySQL 4.1
PHP 4.3.10
OS Win98
IE 6.0
The scripts are very simple and short and the runs in localhost, one simple pc:
In my.ini the defaul-character set was: latin1
1) I created a table in MySQL via command line:
mysql> CREATE TABLE gato2 (nombre VARCHAR(20), apodo(20)) CHARACTER SET utf8 COLLATE utf8_spanish_ci;
Supossely with this I am putting character set "utf8" -the best and universal character set- to all the table, independently of the default character set that appears in my.ini
When I insert spanish characters "ñ, á...") in the table through command line it does it correctly, but when I insert them through a HTML-Php form it does not insert correctly. For example: "Raúl Año", will store as "Ra·l A±o".
2) The HTML Form script "form1.htm" is:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE><Form1</TITLE>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
</HEAD>
<BODY>
<form action="http://localhost/reg2.php" method="post">
Nombre: <input type="text" name="nombre" size="30"></br>
Apellido Paterno: <input type="text" name="apodo" size="30"></br>
<input type="submit" value="Enviar!" size="30">
</form>
</BODY>
</HTML>
3) And the script of PHP File "reg2.php" is:
<?php
header("Content-Type: text/html; charset=utf-8");
//Aqui se conesta con el servidor y la base de datos
include 'cox.php';
include 'bd_meusers.php';
//Aqui se define las variables del formulario que pasare a la BD con este .Php
$nombre=$_POST['nombre'];
$apodo=$_POST['apodo'];
//Y aqui introduzco estos datos en la base de datos
$sql = mysql_query("INSERT INTO gato2 (nombre, apodo) VALUES('$nombre', '$apodo')") or die(mysql_error());
?>
--------------
That is all! It is rare that it does not work if the scripts are simple and I am following the steps of the MySQL nd forum steps. Finally I changed in my.ini the default-character-set from latin1 to utf8:
default-character-set=utf8
but the problem does not dissapear: MySQL does not recognize spanish letter "ñ" and accents á, é...
What is the problem, with the scripts or configuration please? I think the problem is in PHP, because I can insert correctly those special characters when I do with command lines... I have tried and read a lot and also your: [webmasterworld.com...]
and ask many people, but there is no solution by now. It seems it is a common problem in Spanish programmation... Can anyone help with this please?
Thank you very much!
Percy
Depending on how the data is ultimately used, you may be able to get by with:
$nombre=htmlentities($_POST['nombre']); $apodo=htmlentities($_POST['apodo']); This is useful if, for example, you are storing user names and such in a db then using that data to display things on a web page, like the name stored in the db.
********
3) And the script of PHP File "reg2.php" is:
<?php
header("Content-Type: text/html; charset=utf-8");
//Here database and server are connected
include 'cox.php';
include 'bd_meusers.php';
//Here I will define form variables that will be inserted to MySQL database-table through this PHP file
$nombre=$_POST['nombre'];
$apodo=$_POST['apodo'];
//And here I insert this data in the database
$sql = mysql_query("INSERT INTO gato2 (nombre, apodo) VALUES('$nombre', '$apodo')") or die(mysql_error());
?>
*******
I hope this incompatibility of Spanish special characters may have solution, thanks a lot!
*******
Hi StupidScript and thank you! I have done what you suggested: write HTMLENTITIES in the $_POST:
$nombre=htmlentities($_POST['nombre']);
$apodo=htmlentities($_POST['apodo']);
but the result in the MySQL table is still incorrect:
When I insert "Raúl Año" in the html form these are the results shown in the MySQL table:
PHP without HTMLENTITIES: "Ra·l A±o"
PHP without HTMLENTITIES: "RaÃordm;l Año"
Thank you very much! Hope to get your comment!
Percy
Have some reading
[us2.php.net...]
Note that
htmlentities ( string string [, int quote_style [, string charset]] )
it takes an optional third argument charset which defines character set used in conversion. [...] Presently, the ISO-8859-1 character set is used as the default.
I need that the data inserted in the table muss be shown like it was insertedNO NO! Don't reinvent the wheel...
What if a user type the following in your form
my'name"Then, this will definitely mess up your SQL query unless you change the ' and the " (which are special characters)...
htmlentities() is a must! Use it...
If you want to do without it, then you'll definitely have problems (safety, special characters, etc...)
Yeah, data will be storeb with special characters encrypted.
FORM -> CHECK DATA -> ENCRYPT DATA -> INSERT SQL
RETRIEVE SQL -> DECRYPT DATA -> SHOW DATA
It looks like you don't want to use an HTML page to view the contents of the database.
You want some users to be able to view the data directly IN the database, and that's why you are worried about the entity encoding, right?
This is a tough one, because the special characters you are talking about are, indeed, special.
In order for any type of database (except a text file) to handle special characters, they must be encoded in some fashion, either as a unicode string or as something else. When you are using a program like, say, Microsoft Access, it hides the encoding from you, and does the decoding 'on-the-fly' when you are viewing data from within the program. If you were able to see the actual data, as you are with MySQL and others, you would see the encoding instead of the user-readable decoded version the program show you by default.
I have two comments and one suggestion.
First comment, from a security and data protection standpoint, it is a good idea to have very few users actually manipulating the database directly. Within the database there are very few checks for corrupted data or opportunities to keep big mistakes from happening, like deleting too much data or even an entire table by accident.
Second comment, it is not very efficient to work directly with the table data when it is not hard to create an interface to it.
My suggestion is to go ahead and use
htmlentities() and (if you're running PHP 5+) html_entity_decode() to manage ALL user input, regardless of whether it might contain special characters. This will help protect your database from bad data, and handle the proper encoding for your application. Build an interface to the database using PHP, and set up a user and group permissions system to make sure that only the users you want are manipulating the data. In this interface, the PHP will be handling the encoding and decoding, so the safely encoded data in the database will not be a problem for you or your users.
It's more work, but it's the right thing to do.
Here is the basic idea for encoding and decoding. I'm using separate pages for each step to keep the explanation simple, but I would usually do everything on one page using conditional statements.
1: The page with the form for entering data
This is a normal web page with a normal FORM element. The FORM's 'action' points to the processing page.
<form action='processform.php' method=post> 2: The form processing and database entry page
This page (a) checks for form data coming in under the POST method, (b) encodes the form data and (c) enters the encoded data into the database.
if (is_set($_POST['nombre'])) { $nombre=htmlentities($_POST['nombre']); $dbIn=mysql_query("insert into db values ... etc. You probably want to redirect the user to a confirmation page or something after a successful db dump.
3: The database interface page.
First, establish a login function using any method you like. That task is more than I want to include in this example, but it's not hard and many threads here can help you.
Second, get the data from the database and decode it for use in an email message. It is not necessary to decode the data if you are going to simply echo it in HTML page output, because the encoding works just fine in a web browser.
$dbOut=mysql_query("select nombre from db where ..."); while($row=mysql_fetch_array($dbOut)) { $nombre=html_entity_decode($row["nombre"]);
etc. Then comes the mail function ... also found in many threads in this forum.
I hope that helps you understand the process:
1: Data posted
2: Data encoded and entered into db
3: Data retrieved from db and decoded