I'm having trouble to insert specific symbols in the database. I think the problem is related to PDO.
I've been trying to insert using prepare() and execute(), and I discovered that something is changing the symbols(and sometimes, making the insertion fail). The symbols are ?¿!¡. I tried, also, using just exec(), but the problem persist. A friend told me that PDO is sanitizing my query because SQL injection so it takes away characters, sometimes it replaces them and sometimes not let the insertion. I didn't found any info about it. I dont know if that is what is happening. If that's the case: how may I establish some kind of rules?
<?php
/* Here I establish connection with the database, nothing wrong here */
$dsn = 'mysql:dbname=dbname;host=localhost;charset=utf8';
$usuario = 'user';
$contraseña = 'password';
try {
$conn = new PDO($dsn, $usuario, $contraseña);
} catch (PDOException $e) {
echo 'Something went wrong, Mkway?';
}
/*Oh! I forgot! Another thing I tried is changing character set, but it didn't
work either(I think that this is the same that specifing the charset, frankly
idk)*/
$conn->exec("SET CHARACTER SET utf8");
$answer = '¿¡Alguien dijo comer!?'; //It's a literal string. In the real code
this is an input, but here I'll suppose this is the input so you understand
the issue(before the idea occurs to you: I tried specifing this input and
checking if the insertion was successful, but it didn't work too)
$sth = $conn->prepare("INSERT INTO Answer (text, next) VALUES (:answer, 1)");
//So here I prepare the statement, nothing wrong with the query, tried it in
the database with the answer variable and worked like a charm.
$args = [
':answer' => $answer
]; //Here the arguments I'll will include in the execute.
$sth->execute($args); //So here is the execute. I tried an exec, but it didn't
work too.
So, what I'm trying to do is that when i try to insert ¿¡Alguien dijo comer!? it actually inserts it, right now it's not inserting that. I tried too inserting Alguien dijo comer!? and it inserted Alguien dijo comer!r so it replaced the ? for r. Do you know what's wrong here?