我正在尝试在我的数据库表中插入一些数据,但是当我尝试在货币列中保存欧元符号时,我得到了这个例外:
PDOException: SQLSTATE[HY000]: General error: 1366
Incorrect string value: '\x80' for column 'currency' at row 1
如果我不使用任何符号,它可以正常工作。抛出异常并且数据在数据库表中正确写入。
这就是我正在使用的技术堆栈:
我试图谷歌解决方案,但没有任何方法可以帮助我:
但没有任何帮助...
这是我的表的创建脚本,我现在正在使用:
CREATE TABLE `cars` (
`car_id` int(11) NOT NULL AUTO_INCREMENT,
`brand` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`model` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`car_trim` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`model_year` int(11) DEFAULT NULL,
`car_condition` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`car_type` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`currency` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL, <-- Where the € should be
PRIMARY KEY (`xy`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_unicode_ci;
我的PDO连接如下所示:
if(!isset(self::$connection)) {
try {
self::$connection = new PDO('mysql:host='.self::$config['host'].
';dbname='.self::$config['dbname'].
';port='.self::$config['port'].
';charset='.self::$config['charset'], //utf8mb4
self::$config['username'],
self::$config['password']
);
self::$connection->setAttribute( PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION );
} catch (PDOException $e) {
self::$logger->LogError('Connection.php: ' . $e->getMessage());
echo $e->getMessage();
self::$connection->rollBack();
$this->closeConnection();
}
}
带有insert语句的PHP代码:
$db = new Db();
$car = new Car($_POST['carBrand'], $_POST['carModel'], $_POST['carTrim'],
$_POST['carModelYear'], $_POST['carCondition'],
$_POST['carType'], $_POST['carFuelType'], $_POST['carTransmission'],
$_POST['carEngine'], $_POST['carCylinder'],
$_POST['carMileage'], $_POST['carExteriorColor'],
$_POST['carInteriorColor'], $_POST['carLocation'], $_POST['carVin'],
$_POST['carDriveTrain'], $_POST['carStock'], $_POST['carPrice'],
$_POST['carPriceDetails'], $_POST['carCurrency'],
$_POST['carTax'], $_POST['carTaxDetails'], $_POST['carDescr'],
$_POST['carBodyDescr'], $_POST['carDriveTrainDescr'],
$_POST['carExteriorDescr'], $_POST['carElectronicsDescr'],
$_POST['carSaftyFeaturesDescr'], $_POST['carSpecialFeaturesDescr'],
null, null, null, null, null, null, null, null, null, null, 'Edi', 'Edi',
(empty($_POST['carOnline'])) ? 'off' : 'on');
$car_insertStm = 'INSERT INTO cardealer.cars ( ' .
'brand, model,car_trim,model_year,car_condition,car_type, fuel_type, transmission, ' .
'car_engine, cylinder, mileage, exterior_color, interior_color, location, vin, drive_train, ' .
'stock, price, price_descr, currency, tax, tax_descr, car_descr_long, body_descr, ' .
drive_train_descr, exterior_descr, electronics_descr, safty_features_descr, special_features_descr, car_pic_1, car_pic_2, car_pic_3, ' .
'car_pic_4, car_pic_5, car_pic_6, car_pic_7, car_pic_8, car_pic_9, car_pic_10, create_user, ' .
'change_user, car_online ) ' .
'VALUES ( :brand, :model, :car_trim, :model_year, :car_condition, :car_type, :fuel_type, :transmission, :car_engine, :cylinder, ' .
':mileage, :exterior_color, :interior_color, :location, :vin, :drive_train,
:stock, :price, :price_descr, :currency, :tax, :tax_descr, ' .
':car_descr_long, :body_descr, :drive_train_descr, :exterior_descr,
:electronics_descr, :safty_features_descr, :special_features_descr, ' .
':car_pic_1, :car_pic_2, :car_pic_3, :car_pic_4, :car_pic_5, :car_pic_6,
:car_pic_7, :car_pic_8, :car_pic_9, :car_pic_10, :create_user, ' .
':change_user, :online ) ';
$pStatement = $db->getConnection()->prepare($car_insertStm);
$pStatement->bindParam(':brand', $_POST['carBrand'], PDO::PARAM_STR);
$pStatement->bindParam(':model', $_POST['carModel'], PDO::PARAM_STR);
$pStatement->bindParam(':car_trim', $_POST['carTrim'], PDO::PARAM_STR);
$pStatement->bindParam(':model_year',
$_POST['carModelYear'],PDO::PARAM_INT);
$pStatement->bindParam(':car_condition',
$_POST['carCondition'], PDO::PARAM_STR);
$pStatement->bindParam(':car_type', $_POST['carType'], PDO::PARAM_STR);
$pStatement->bindParam(':fuel_type', $_POST['carFuelType'], PDO::PARAM_STR);
$pStatement->bindParam(':transmission',
$_POST['carTransmission'], PDO::PARAM_STR);
$pStatement->bindParam(':car_engine', $_POST['carEngine'], PDO::PARAM_STR);
$pStatement->bindParam(':cylinder', $_POST['carCylinder'], PDO::PARAM_STR);
$pStatement->bindParam(':mileage', $_POST['carMileage'],PDO::PARAM_INT);
$pStatement->bindParam(':exterior_color',
$_POST['carExteriorColor'], PDO::PARAM_STR);
$pStatement->bindParam(':interior_color',
$_POST['carInteriorColor'], PDO::PARAM_STR);
$pStatement->bindParam(':location', $_POST['carLocation'], PDO::PARAM_STR);
$pStatement->bindParam(':vin', $_POST['carVin'], PDO::PARAM_STR );
$pStatement->bindParam(':drive_train',
$_POST['carDriveTrain'], PDO::PARAM_STR);
$pStatement->bindParam(':stock', $_POST['carStock'], PDO::PARAM_STR);
$pStatement->bindParam(':price', $_POST['carPrice'], PDO::PARAM_STR);
$pStatement->bindParam(':price_descr',
$_POST['carPriceDetails'], PDO::PARAM_STR);
$pStatement->bindParam(':currency', $_POST['carCurrency'], PDO::PARAM_STR); // Parameter binding for the currency --> €
$pStatement->bindParam(':tax', $_POST['carTax'],PDO::PARAM_INT);
$pStatement->bindParam(':tax_descr',
$_POST['carTaxDetails'], PDO::PARAM_STR);
$pStatement->bindParam(':car_descr_long',
$_POST['carDescr'], PDO::PARAM_STR);
$pStatement->bindParam(':body_descr',
$_POST['carBodyDescr'], PDO::PARAM_STR);
$pStatement->bindParam(':drive_train_descr',
$_POST['carDriveTrainDescr'], PDO::PARAM_STR);
$pStatement->bindParam(':exterior_descr',
$_POST['carExteriorDescr'], PDO::PARAM_STR);
$pStatement->bindParam(':electronics_descr',
$_POST['carElectronicsDescr'], PDO::PARAM_STR);
$pStatement->bindParam(':safty_features_descr',
$_POST['carSaftyFeaturesDescr'], PDO::PARAM_STR);
$pStatement->bindParam(':special_features_descr',
$_POST['carSpecialFeaturesDescr'], PDO::PARAM_STR);
$pStatement->bindValue(':car_pic_1', null, PDO::PARAM_LOB);
$pStatement->bindValue(':car_pic_2', null, PDO::PARAM_LOB);
$pStatement->bindValue(':car_pic_3', null, PDO::PARAM_LOB);
$pStatement->bindValue(':car_pic_4', null, PDO::PARAM_LOB);
$pStatement->bindValue(':car_pic_5', null, PDO::PARAM_LOB);
$pStatement->bindValue(':car_pic_6', null, PDO::PARAM_LOB);
$pStatement->bindValue(':car_pic_7', null, PDO::PARAM_LOB);
$pStatement->bindValue(':car_pic_8', null, PDO::PARAM_LOB);
$pStatement->bindValue(':car_pic_9', null, PDO::PARAM_LOB);
$pStatement->bindValue(':car_pic_10', null, PDO::PARAM_LOB);
$pStatement->bindValue(':create_user', "Edi", PDO::PARAM_STR);
$pStatement->bindValue(':change_user', "Edi", PDO::PARAM_STR);
$pStatement->bindValue(':online',
(empty($_POST['carOnline'])) ? "off" : "on",
PDO::PARAM_STR);
$result = $pStatement->execute();
如果有人可以帮助我摆脱那个消息,那将是非常好的。
每一个帮助都非常有用。
提前致谢。
答案 0 :(得分:0)
我有一个解决方案,我不知道它是否完美,但至少它工作正常!
我在打开数据库连接后添加了这行代码:
$pStatement = $db->getConnection()->query("SET CHARACTER SET utf8");
在此查询之后,我可以插入我想要的内容(例如,ÄÜÖö䧀%$'#etc ...),它最终会正确存储在我的表格中。
好的,谢谢你试图帮助我。我希望这个解决方案可以帮助其他有同样问题的人。
祝大家新年快乐。 :-)