如果有人能够帮我解决将xml文件中的数据插入数据库的问题,我将不胜感激。我想防止在刷新页面后在数据库中添加重复行。这里是我的代码:加载xml文件时不要插入重复的行
public function getAll() {
$xml = new DOMDocument();
$xml->load('newXMLDocument.xml');
$xmldata = $xml->getElementsByTagName('book');
$xmlcount = $xmldata->length;
for($i=0; $i< $xmlcount; $i++){
$author = $xmldata->item($i)->getElementsByTagName('author')->item(0)->childNodes->item(0)->nodeValue;
$name = $xmldata->item($i)->getElementsByTagName('name')->item(0)->childNodes->item(0)->nodeValue;
try {
$statement = self::$db->prepare("INSERT INTO `books`(`Author`, `Name`) values(?, ?) ");
$statement->bindParam(1, $id);
$statement->bindParam(2, $author);
$statement->bindParam(3, $name);
$statement->execute(array(
$author,
$name
));
} catch (PDOException $e) {
echo $e->getMessage();
}
}
}
运行插入查询Downvoted无法正确格式化您的代码。 –