Spoonk
Spoonk

Reputation: 703

Simple HTML form with POST to SQL function encoding problem?

I have a simple html form that submits information with POST function. But when information contains a Cyrillic characters, in table in MySql there becomes азазаза symbols instead of text. The table is on utf-8_general_ci, the site is on UTF-8 encoding. I visualize the result from this table with

$query = "
  SELECT ".$db->nameQuote('ingredients')."
    FROM ".$db->nameQuote('other')."
    ORDER by id DESC
  ";
$db->setQuery($query);
$ingredients = $db->loadResult();

I cant understand how to tell the form to send chyrillic characters correct. Or where is the problem at all? How to fetch this characters correctly? Or how to send them correctly?

-----------------EDIT-----------------------

I couldn't understand where to put

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

So I'm pasting my code here. First the simple form:

<form action="insert.php" method="post" onsubmit="return checkForm(this)" target="_top">
<table>

<tr>
<td colspan="2">
<ul>
<li> Добавете необходимите за рецептата съставки</li>
<li> Моля попълнете всички полета коректно</li>
<li> Полетата маркирани с (*) са задължителни</li>
</ul>
</td>
</tr>

<tr>
<td>
Количество (порции)*: 
</td>
<td>
<input type="text" name="quantity" />
</td>
</tr>

<tr>
<td>
Съставки*: 
</td>
<td>
<input type="text" name="ingredients" />
</td>
</tr>

<tr>
<td>
Време за приготвяне*:
</td>
<td>
<input type="text" name="timing" /><br />
</td>
</tr>

<tr>
<td></td>
<td>
<input type="submit" value="Напред" class="button validate" />
</td>
</tr>
</table>
</form>

And the fetching syntax inside my insert.php file:

$query = "
  SELECT ".$db->nameQuote('quantity')."
    FROM ".$db->nameQuote('other')."
    ORDER by id DESC

  ";
$db->setQuery($query);
$quantity = $db->loadResult();

$query = "
  SELECT ".$db->nameQuote('ingredients')."
    FROM ".$db->nameQuote('other')."
    ORDER by id DESC
  ";
$db->setQuery($query);
$ingredients = $db->loadResult();

$query = "
  SELECT ".$db->nameQuote('timing')."
    FROM ".$db->nameQuote('other')."
    ORDER by id DESC
  ";
$db->setQuery($query);
$timing = $db->loadResult();

Upvotes: 1

Views: 1005

Answers (5)

Tore A.
Tore A.

Reputation: 629

Your problem may be the browser rather than the database. You should encode the string to utf8 before inserting it into the database to make sure.

Upvotes: 0

Your Common Sense
Your Common Sense

Reputation: 157839

you have to put only one query, SET NAMES one, in your $db class, right after select db statement. using not mysql_query() function but one you're using to execute all other queries.

can you post here the connect statement used in your db class?

Upvotes: 0

Ain Tohvri
Ain Tohvri

Reputation: 3035

Double check your database. I've used the utf8_unicode_ci collation for Russian, Estonian (umlauts) etc. and made sure the forms get posted in UTF-8 as well, no problems.

And if you keep some multibyte UTF-8 stuff hardcoded inside the PHP, it's best to make sure the internal PHP encoding is appropriate as well:

mb_internal_encoding("UTF-8");
mb_http_output("UTF-8");
ob_start("mb_output_handler");

And yes, as guys stated here, I'm also using the

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

in the database class conditionally for MySQL 4.x, but not for MySQL 5. Works fine there, it was a bug reported for 4.1 if I recall correctly.

Upvotes: 0

Robus
Robus

Reputation: 8259

Try setting collocation?

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

Upvotes: 1

Artefacto
Artefacto

Reputation: 97805

азазаза shows up because you're interpreting a UTF-8 bytestream containing азазаза as ISO-8859-1. Make sure that you set the client encoding to UTF-8, so that the database knows you're sending it UTF-8 bytestreams.

Issue the statement

SET NAMES UTF8

Upvotes: 0

Related Questions