我有一堂课:
class Media {
private $media;
private $thumb;
private $slug;
private $info;
private $type;
private $link;
}
我尝试使用 PHP PDO 将其保存到数据库中:
$PDO = new PDO("mysql:host=".DBHOST.";dbname=".DB, DBUSER, DBPASS);
$options = array('media' => 'image.jpg',
'thumb' => 'image_thumb.jpg');
$media = new Media($options);
$media = (array)$media;
$STH = $PDO->prepare('INSERT INTO media (media, thumb, slug, info, type, link) values (?, ?, ?, ?, ?, ? )');
$STH->bindParam(1, $media['Mediamedia']);
$STH->bindParam(2, $media['Mediaslug']);
$STH->bindParam(3, serialize($media['Mediainfo'])); //line 150
$STH->bindParam(4, $media['Mediathumb']);
$STH->bindParam(5, $media['Mediatype']);
$STH->bindParam(6, $media['Medialink']);
$STH->execute();
上面的代码产生 1 个错误:
注意:未定义的索引:第 150 行 C:\wamp32\www\MM\index.php 中的 Mediainfo
但print_r($media);
输出:
Array
(
[Mediamedia] => image.jpg
[Mediathumb] => image_thumb.jpg
[Mediaslug] => image
[Mediainfo] => Array
(
[title] => image.jpg
[alt] => image.jpg
[description] => image.jpg
)
[Mediatype] => .jpg
[Medialink] => 0
)
编辑:修复了第二个错误,就像@dleiftah 所说的那样愚蠢,但仍然存在。