HTTP post doesn't send special characters correctly - php

I have two .php files, a form with a text field where you put the names you want to search in the database, and a results file that processes the post...
The names in the database will be searched using this query:
SELECT * FROM acw_papers_web web
INNER JOIN acw_papers_web_autores aut
ON web.id_paper_web = aut.id_paper_web
WHERE aut.nombre_autor_pw LIKE '%autorname%'
ORDER BY web.probabilidad DESC
The problem is that when I send the post, insted of sending lópez, it sends lópez...
How can I fix it... both .php files are utf-8 encoded...

Since you did not provide much information, I cannot exactly pinpoint the problem. But here are two possible solutions:
Set the correct charset in the <head> of both files:
<meta charset="UTF-8" /> for HTML5 or <meta http-eqiv="Content-Type" content="text/html; charset=UTF-8" /> for everything else
Set the character set of the database tables to UTF-8. In MySQL:
ALTER TABLE acw_papers_web CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE acw_papers_web_autores CHARACTER SET utf8 COLLATE utf8_general_ci;
That's all I could think of, for now.

You need to use
urlencode() - URL-encodes string
urldecode() - URL-decode
Functions to send special characters in the Post Request.

Related

Wrong utf8 encoding exporting Mysql database

I always seen in phpmyadmin special characters encoded like:
میثم ابراهیمیجØعشق ØŒ سر ب راه ØŒ نبض ØŒ شبهای ØŒ غنچه ها ØŒ شکوه ØŒ همین امروز ØŒ عادت ت'
I always thought that was just a problem related to phpmyadmin since on my application all of them were displayed correctly.
Now I'm exporting this database and in my mysql dump I see exactly the characters above so seems that they are stored in this way on the database.
There is an easy way to dump the utf8 characters?
I already tried to follow those suggestion: I need help fixing Broken UTF8 encoding
but the only thing that let visualize the characters properly is print them on a web page and add on top:
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
The collation of the fields is utf8_unicode_ci.
The connection exploit:
$options = array(\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',);
Edit: #Álvaro-González Attemp to retrieve hexadecimal values
I enter in the database the symbol € then from phpmyadmin I select with: SELECT name, HEX(name) as hex_name FROM `items`
that's the result:
name €
hex_name C3A2E2809AC2AC
I will provide any further information on request
Thanks
You wanted €? But the hex is C3A2E2809AC2AC. That is a case of "double encoding" - when you stored the data.
See Trouble with utf8 characters; what I see is not what I stored , especially in the discussion about "double encoding".
The data is broken. You may be able to fix the data with a messy UPDATE. See http://mysql.rjweb.org/doc.php/charcoll#example_of_double_encoding .
Edit
Your original stuff looks like
CONVERT(BINARY(CONVERT('میثم اب' USING latin1)) USING utf8mb4)میثم اب -- mojibake to ut8,

SELECT from data_base utf-8_bin data purified

All I want is to SELECT an utf8_bin string stored in an table with 2 rows
1 id and 2 continut first is an int(20) NOT NULL AUTO INCREMENT and second is VARCHAR(2000) utf8_bin NOT NULL used for Romanian language inserts.
The data is stored correct whe i acces it from phpmyadmin, but on echo it returns strange characters instead romanian diacritics.
$sqlis = "SELECT continut FROM cantari WHERE id = {$id}";
$dbh->query($sqlis);
foreach ($dbh->query($sqlis) as $liniie);
$continut = $liniie['continut'];
This is the result: Cau?i r�uri ?i izvoare
$continut is my data from sql. I've setted meta on file as utf8 <meta charset="utf-8"> in header's content.
Can htaccess help me or css? or how to replace that elements with the normal Romanian diacritics?
Have you set the character set to UTF-8 on the Mysql connection?
For example:
$dbh->set_charset("utf8")
Without it, the data returned from MySQL will be translated to Cp1252. Any chars that don't fit in Cp1252 will be set to "?".
$sqlis = "SELECT continut FROM cantari WHERE id = {$id}; SET NAMES 'utf8' ";
So all we need is to specify in SQL server side that we will use utf8 characters.

cyrillized words read from database with php

Why when I'm reading a cyrillic text from database it's ok , but when I put this text in select-option menu I get strange symbols
http://prikachi.com/images/813/6589813g.jpg
http://prikachi.com/images/811/6589811I.jpg
I think that I put everywhere to be utf-8 but I don't know ...
in my html I use :
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
most likely you're not using the same charset (utf-8) everywhere so your data gets messed up at some point. depending on what exactly you're doing, you'll have to change/add one or more of the following points (maybe it's the SET CHARSET/mysql_set_charset you forgot):
tell MySQL to use utf-8. to do this, add this to your my.cnf:
collation_server = utf8_unicode_ci
character_set_server = utf8
before interacting with mysql, send this two querys:
SET NAMES 'utf8';
CHARSET 'utf8';
or, alternatively, let php do this after opening the connection:
mysql_set_charset('utf8', $conn); // when using the mysql_-functions
mysqli::set_charset('utf8') // when using mysqli
set UTF-8 as the default charset for your database
CREATE DATABASE `my_db` DEFAULT CHARACTER SET 'utf8';
do the same for tables:
CREATE TABLE `my_table` (
-- ...
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
assuming the client is a browser, serve your content as utf-8 and the the correct header:
header('Content-type: text/html; charset=utf-8');
to be really sure the browser understands, add a meta-tag:
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
and, last but not least, tell the browser to submit forms using utf-8
<form accept-charset="utf-8" ...>

PDO utf8_encoding my text string twice in INSERT?

Relevent code:
$status = $db->run(
"INSERT INTO user_wall (accountID, fromID, text, datetime) VALUES (:toID, :fromID, :text, '" . time() . "')",
array(":toID" => $toID, ":fromID" => %accountID, ":text" => $text)
);
I am taking input text from javascript, throwing it in an AJAX call to handle it, which calls a function which includes these lines of code.
The text string in question is: "Türkçe Türkçe Türkçe!"
Upon investigating the database, the following value is saved "Türkçe Türkçe Türkçe!", which is double utf8_encode'd.
When viewing the text by SELECTing it from the database, I get "Türkçe Türkçe Türkçe!", which is how they should be saved in the database in the first place.
As far as I know, I am not encoding the data as it is being prepared by PDO...
Encoding is a b*tch. You need to make sure it is as you expect it to be in several places:
The HTML page with the Javascript. Set it to utf-8 with a meta tag like:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
The connection to your database. Execute the query set names 'utf8' after connecting to the database (and before any other queries).
Your database field. In MySQL it's called a collation set it to utf8_general_ci (ci stands for case-insensitive).
If you have these 3 your data should always be, and stay, utf-8 (unless you're doing encoding yourself).
For good measure, make sure your source code files are utf-8 as well. Windows typically defaults to iso.

php 5.2 + mysql 5.1 character encoding issue

Background:
There is a table, events; this table is formatted latin1. Individual columns in this table are set to utf8. The column we will cherry pick to discuss is 'title' which is one of the utf8 columns. The website is set for utf8 both via apache and the meta tag.
As a test, if I save décor or © into the title field and perform
select title, LENGTH(title) as len, CHAR_LENGTH(title) as chlen
from events where length(title) != char_length(title)
I will get décor or ©, 12, 10 back as a result; which is expected showing that the data has indeed been properly saved into my utf8 column.
However, upon echoing the title out to a page, it's mangeld into d�cor or � which makes no sense to me since, as mentioned before, the character encoding is set to utf-8 on the page.
Not sure if this final detail makes a difference but if I edit the page and resubmit the mangled text it turns into d%uFFFDcor or %uFFFD both in the database and when displayed to the page. Further submits cause no change.
Actual Question:
Does anyone have an idea as to what I may be doing wrong? :-P
Well, there's likely one of three problems.
1. Mysql's connection is not using UTF-8
This means that it's converted to another charset (likely Latin-1) before it hits PHP. I've found the best solution is to run the following queries:
SET CHARACTER SET = "utf8";
SET character_set_database = "utf8";
SET character_set_connection = "utf8";
SET character_set_server = "utf8";
2. The page rendered is not really set to UTF-8
Set both the Content-type header and the <meta> tag content types to UTF-8. Some browsers don't respect one or the other...
header ('Content-Type: text/html; charset=UTF-8');
echo '<meta http-equiv="content-type" content="text/html; charset=utf-8" />';
As noted in the comments, that's not the problem...
3. You're doing something to the string before echoing it
Most of PHP's string functions will not do well with UTF-8. If you're calling a normal function that doesn't accept a $charset parameter, the chances are that it won't work with utf-8 strings (such as str_replace). If it does have a $charset parameter (like htmlspecialchars, make sure that you set it.
echo htmlspecialchars($content, ENT_COMPAT, 'UTF-8');

Categories