0

I have a table with answers like that:

_________________________
| id | correct |  answer |
|____|_________|_________|
|  1 |   1     |   اللَّيلِ   |
|____|_________|_________|
|  2 |   0     |   النَّهارِ   |
|____|_________|_________|

I display these answers and then I submit one of them with Ajax:

const request = new XMLHttpRequest();
request.open('POST', 'submit.php', true);
request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
request.responseType = 'json';
var data = 'answer=' + userAnswer;
request.send(data);

Then I check it against DB:

$answer = $_POST['answer'];
$correctAnswer = 'SELECT answer FROM answers WHERE correct = 1';
if ($correctAnswer == $answer) {
    echo 'equal';
}else{
    echo 'not equal';
}

On safari when I submit the correct answer I get 'not equal'. This is the submitted string from safari اللَّيلِ. When I compare this string to the one in the DB they are not equal:

$userAnswer = 'اللَّيلِ';
$correctAnswer = 'اللَّيلِ';
if ($userAnswer == $correctAnswer) {
    echo 'equal';
}else{
    echo 'not equal';
}

I use utf-8 encoding for both HTML and DB connection:

<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>

$conn = new PDO("mysql:host=$servername;dbname=$db;charset=UTF8", $username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

What's wrong? why the same record from DB is different?

Markus Zeller
  • 8,516
  • 2
  • 29
  • 35
mmdev
  • 33
  • 6
  • 1) You should really URL-encode the data you send via XHR. 2) Have you read https://stackoverflow.com/questions/279170/utf-8-all-the-way-through? – ADyson Oct 12 '22 at 11:44
  • You set the encoding inside the html page which let the browser know how to "show" what's in it. On the page that **receive** the data you should set the encoding in the http header. header('Content-Type: text/html; charset=utf-8'); This tell the browser how to encode the data it is going to send. This header(); must be before any output (ie echo) is generated, otherwise you will get an error like the following: Warning: Cannot modify header information - headers already sent by (output started... Check database encoding, which for arabic (on mysql) should be utf8mb4 – Roberto Braga Oct 12 '22 at 11:54
  • You should probably look into "normalization" - https://stackoverflow.com/questions/7931204/what-is-normalized-utf-8-all-about – CBroe Oct 12 '22 at 12:06
  • I tried to add encoding while sending the request `request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=utf-8');` but still the same issue – mmdev Oct 12 '22 at 13:41
  • That isn't a valid content-type I don't think. You can add that for a JSON type, but not for form-url-encoded (as far as I know). But that alsmost certainly isn't the issue though, or if it is, it's only one part of it. Read the link I gave you earlier and make sure you implement all the recommendations. – ADyson Oct 12 '22 at 13:48
  • @ADyson I followed it. DB connection/HTML meta tag/PHP files all using utf encoding. Still no success – mmdev Oct 12 '22 at 14:06
  • You seem to be using a different character encoding for your database than the one the guide recommended – ADyson Oct 12 '22 at 14:10
  • @ADyson I tried both `utf8mb4_general_ci` and `utf8_general_ci` and for db connection I tried `utf8` and `utf8mb4`. None worked – mmdev Oct 12 '22 at 14:21
  • The strings from your code examples are not the same. See difference here: https://3v4l.org/0kNfR – Markus Zeller Oct 12 '22 at 14:57
  • @MarkusZeller I see but that's strange as I fetch it from DB and compare it against the same record so it should be the same – mmdev Oct 12 '22 at 15:04
  • You should make all the necessary changes and then try inputting new data, and then try to read it back. Existing data may or may not have been corrupted in some way, it's best to set yourself up properly and then try again from the beginning. – ADyson Oct 12 '22 at 15:12
  • Maybe you should do a hex encoding on receipt, storing and reading back to compare **where** the problem happens. Probably you need to go with mb_string() functions. – Markus Zeller Oct 12 '22 at 15:13
  • I am little confused with the query `SELECT answer FROM answers WHERE correct = 1`. Do you have really only 1 correct answer stored? – Markus Zeller Oct 12 '22 at 15:15
  • @MarkusZeller Yeay for each question only one is correct – mmdev Oct 12 '22 at 16:04
  • I see no foreign key to the answer. Maybe you get and compare the wrong one? – Markus Zeller Oct 12 '22 at 16:36
  • @MarkusZeller There is a column for question id. I didn't mention it because it won't make a difference here – mmdev Oct 12 '22 at 16:50

0 Answers0