Check alle échte Black Friday-deals Ook zo moe van nepaanbiedingen? Wij laten alleen échte deals zien

[PHP/PDO/MySQL] Speciale caracters

Pagina: 1
Acties:

  • Xerohumoris
  • Registratie: Augustus 2010
  • Laatst online: 19-11 19:51
Hoi allemaal,

Ik zit met een vervelend probleem. Ik probeer namen met speciale tekens op te slaan in een tabel. Nu weet ik dat je moet zorgen dat alle tekenset over het hele systeem gelijk zijn.

De eerste regel van alle code is dan ook.
PHP:
1
header('Content-Type: text/html; charset=utf-8');


In het html document zelf
code:
1
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">


In apache httpd.conf. Gebruik externe beheer tool voor apache en dit is wat ik zie.
code:
1
AddDefaultCharset 'utf-8'


De code waarmee ik het PDO object maak.
PHP:
1
2
3
4
5
6
7
8
9
10
class database extends PDO{

     public function __construct($dbcfg){
        $opt = array(
            PDO::MYSQL_ATTR_INIT_COMMAND =>'SET NAMES utf8',
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        );
        parent::__construct("mysql:host=" . $dbcfg["host"] . ";dbname=" . $dbcfg["database"], $dbcfg["user"], $dbcfg["password"], $opt);
     }
}


De sql structuur en charset
code:
1
2
3
4
5
6
7
8
9
10
11
12
CREATE TABLE IF NOT EXISTS `users` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(255) DEFAULT NULL,
  `lastname` varchar(255) DEFAULT NULL,
  `email` varchar(255) NOT NULL,
  `pass` varchar(128) NOT NULL,
  `language` varchar(3) NOT NULL DEFAULT 'en',
  `role` int(128) NOT NULL,
  `active` tinyint(4) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;


Maar als ik mijn eigen naam in de database wil zetten krijg ik de volgende error:
code:
1
2
3
4
5
6
7
8
[client 127.0.0.1:56660] PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[22007]: Invalid datetime format: 1292 Truncated incorrect DOUBLE value: 'Dani\xc3\xabl'' in /srv/(...)/database.php:119
Stack trace:
#0 /srv/(...)/database.php(119): PDOStatement->execute()
#1 /srv/(...)/account.php(10): database->updateUser('Dani??l', '(...)')
#2 /srv/(...)/index.php(85): account->__construct()
#3 /srv/(...)/index.php(16): start()
#4 {main}
  thrown in /srv/(...)/database.php on line 119, referer: http://127.0.0.1/index.php


Is er iemand die mij kan vertellen wat er fout gaat.

Vriendelijke groet,
Daniël

p.s. (...) Betekend dat ik stukken tekst heb verwijderd die niet van invloed zijn op de error.

  • Feanathiel
  • Registratie: Juni 2007
  • Niet online

Feanathiel

Cup<Coffee>

En hoe ziet je query eruit? De foutmelding "Invalid datetime format: 1292 Truncated incorrect DOUBLE value: 'Dani\xc3\xabl''" zegt namelijk iets over getallen en datums.

  • Xerohumoris
  • Registratie: Augustus 2010
  • Laatst online: 19-11 19:51
Ja vond ik ook raar. Ik bedacht mij dus later dat het handig kon zijn de code toe te voegen. Bij dezen dus.
PHP:
1
2
3
4
5
$query = $this->prepare("UPDATE `users` SET `firstname` = :firstname AND `lastname` = :lastname WHERE `id` = :id");
        $query->bindParam(":firstname", $firstname, PDO::PARAM_STR);
        $query->bindParam(":lastname", $lastname, PDO::PARAM_STR);
        $query->bindParam(":id", $id, PDO::PARAM_INT);
        return $query->execute();


EDIT: Fout gevonden. AND moet , zijn.

[ Voor 4% gewijzigd door Xerohumoris op 08-11-2014 17:33 ]