function insert($name, $desc, $year, $rating, $poster, $trailer, $category_id) {
$mysqli = new mysqli('localhost', 'root', '', 'kinomonster');
if (mysqli_connect_errno()) {
printf("Соединение не установлено!", mysqli_connect_error());
exit();
}
$mysqli->set_charset('utf8');
$query = "INSERT INTO movies VALUES(null, '$name', '$desc', '$year', '$rating', '$poster', '$trailer', Now(), '$category_id')";
$result = false;
if ($mysqli->query($query)) {
$result = true;
}
return $result;
}
$movies = simplexml_load_file("xml/movies_1.xml") or die("Error: Cannot create object");
$title = null;
$title_origin = null;
$year = null;
$rating = null;
$post = null;
$trailer = null;
foreach ($movies as $movie_key => $movie) {
$title = $movie->title_russian_english;
$title_origin = $movie->desc;
$year = $movie->year;
foreach ($movie->poster->big->attributes() as $poster_key => $poster) {
$post = $poster;
}
if ($movie->kinopoisk) {
$rating = $movie->kinopoisk->attributes()['rating'];
}
else {
$rating = null;
}
foreach ($movie->trailer->attributes() as $trailer_key => $trailerXML) {
$trailer = $trailerXML;
}
if ($movie->trailer) {
$trailer = $movie->trailer->attributes()['url'];
}
else {
$trailer = null;
}
if (insert($title, $title_origin, $year, $rating, $post, $trailer, 1)) {
echo "ДАННЫЕ УСПЕШНО ВСТАВИЛИСЬ!!!<br>";
}
else {
echo "ДАННЫЕ НЕ ВСТАВИЛИСЬ!!!<br>";
}
}
echo "<pre>";
print_r($movies);
echo "</pre>";