q2a / question2answer

Question2Answer is a free and open source platform for Q&A sites, running on PHP/MySQL.
http://www.question2answer.org/
GNU General Public License v3.0
1.63k stars 629 forks source link

event-logger query insertion fails if column 'params' contains utf8 character #777

Closed tangruize closed 4 years ago

tangruize commented 5 years ago

Recently I installed edit-history plug-in, which requires event-logger enabled. However, when I post an answer containing utf8 characters, it shows a message "A Question2Answer database query failed when generating this page." The answer is posted correctly but edit-history doesn't work. And the message is really confusing.

I checked /var/log/apache2/error.log and pick a part of log here:

[Wed Sep 25 20:49:44.757934 2019] [:error] [pid 99922] [client 127.0.0.1:49244] PHP Question2Answer MySQL query error 1366: Incorrect string value: '\\xE6\\xB5...\\x09...' for column 'params' at row 1 - Query: INSERT INTO qa_eventlog (datetime, ipaddress, userid, handle, cookieid, event, params) 

After my attempt, I changed line 173 of qa-plugin/event-logger/qa-event-logger.php from

qa_remote_ip_address(), $userid, $handle, $cookieid, $event, $paramstring

to

qa_remote_ip_address(), $userid, $handle, $cookieid, $event, utf8_encode($paramstring)

It seems that the problem disappered. But I don't know if this modification is right.

phy25 commented 5 years ago

I guess this is an issue with your charset encoding for qa_eventlog?

https://stackoverflow.com/questions/10957238/incorrect-string-value-when-trying-to-insert-utf-8-into-mysql-via-jdbc

tangruize commented 5 years ago

Oh, I think it is my fault. Actually the database was migrated from another site. And everything works well before. I use mysqldump cmd to dump the database. Maybe something wrong when I restore the database. I have tried to change the database encoding, mysql settings and apache2 settings, but I didn't find a good solution. Thank you very much!