2013-05-04 72 views
0

我现在有正确插入数据的工作代码。我正在尝试添加新的movie_Id列。我试图从XML中获取电影ID值并将其与电影标题循环,但现在插入的数据不正确。 movie_name对象有3-4个电影名称。我正在尝试添加电影ID。无法在新列中插入数据

$movie_name = $arrTitle[0]; 
    $xml = simplexml_load_file("http://api.themoviedb.org/2.1/Movie.search/en/xml/myapi/$movie_name"); 
     foreach($xml->movies->movie as $movies){ 
     $arrMovie_id= $movies->id; 
     } 
     $movie_ids= $arrMovie_id[0]; 
    $arrStr = explode(':',$htmlShowTime); 
    $release = substr($arrStr[3],0,strlen($arrStr[3])-8); 
    $director = substr($arrStr[5],0,strlen($arrStr[5])-11); 

    $sql_movie = "insert into jos_movie(movie_name,language,cast,movie_release,director,rating,rating_count,movie_ids)values('$movie_name','null','$cast','$release','$director',250,230,'$movie_ids')"; 

以下是完整的工作代码

<?php 
// Include the handy XML data extraction functions. 
include 'xml_regex.php'; 
include 'simple_html_dom.php'; 

$con = mysql_connect('localhost','test','test'); 
mysql_select_db('test',$con); 


// Use cURL to get the RSS feed into a PHP string variable. 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,'mytest.xml'); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$xml = curl_exec($ch); 
curl_close($ch); 

$arrData = array(); 
// Create an array of item elements from the XML feed. 
$news_items = element_set('item', $xml); 
$del_movie = "delete from jos_movie"; 
mysql_query($del_movie); 

$del_cinema = "delete from jos_cinema"; 
mysql_query($del_cinema); 

foreach($news_items as $item) { 
    $title = value_in('title', $item); 
    $url = value_in('link', $item); 
    $cast = value_in('description', $item); 
    //curl_setopt($ch, CURLOPT_URL,$url); 
    //curl_setopt($ch, CURLOPT_HEADER, false); 
    //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    //$html = curl_exec($ch); 
    $arrTitle = explode('-',$title); 
    $html = file_get_html($url); 
    $htmlShowTime = ''; 

    // find all span tags with class=gb1 moviTimes moviTmngBox 
    foreach($html->find('ul[style=line-height:2em;]') as $e) 
     $htmlShowTime = $e->plaintext; 

    $movie_name = $arrTitle[0]; 
    $arrStr = explode(':',$htmlShowTime); 
    $release = substr($arrStr[3],0,strlen($arrStr[3])-8); 
    $director = substr($arrStr[5],0,strlen($arrStr[5])-11); 

    $sql_movie = "insert into jos_movie(movie_name,language,cast,movie_release,director,rating,rating_count)values('$movie_name','null','$cast','$release','$director',250,230)"; 
    //echo $sql.'<br>'; 
    //echo $sql_movie; 

    mysql_query($sql_movie); 

    $sqlCount = 'select max(id) from jos_movie'; 
    $data = mysql_query($sqlCount); 
    echo $data; 
    print_r($data); 
    $result = mysql_fetch_array($data); 
    $id = $result[0]; 
    echo '<br>'.$id.'<br>'; 

    //$id = mysql_insert_id(); 
    //echo $id; 

     // find all span tags with class=gb1 
    foreach($html->find('div.moviTmngBox') as $e){ 
     $tagTitle = $e->find('a',0); 
     $tagTime = $e->find('div.moviTimes',0); 
     $name = $tagTitle->title; 
     $time = $tagTime->innertext; 

    $trimName = ''; 
    $temName = strtolower(str_replace(' ','',$name)); 

    if(strpos($temName,'indraaudi1') !== false) 
     $trimName = 'Indra Audi 1' and $cinemaId = '1' and $long='32.726602' and $lat='74.857026'; 
    elseif(strpos($temName,'indraaudi2') !== false) 
    $trimName = 'Indra Audi 2' and $cinemaId = '2'and $long='32.726602' and $lat='74.857026'; 
    elseif(strpos($temName,'indraaudi3') !== false) 
     $trimName = 'Indra Audi 3'and $cinemaId = '3' and $long='32.726602' and $lat='74.857026'; 
    elseif(strpos($temName,'apsra') !== false) 
     $trimName = 'Apsra' and $cinemaId = '4' and $long='32.700314' and $lat='74.858023'; 
    else{ 
     $trimName = trim(substr($name,18,strlen($name))) and $cinemaId = '5' and $long='32.7300' and $lat='74.8700' ; 
    } 

     //echo $tagTime->innertext.'<br/>'; 
     $sql = "insert into jos_cinema(cinema_name,show_time,movie_id,cinemaId,logitude,latitude)values('$trimName','$time',$id,$cinemaId,$long,$lat)"; 
     //echo $sql.'<br/>'; 
     mysql_query($sql); 
     //$arrTem = array($tagTitle->title,$tagTime->innertext); 

    } 

}//end rss feed loop 

?> 

这里是我的要求。

我将$ movie_name中的影片名称作为数组获取 我已将名称附加到xml中,以便我可以从中获取moviedbId。我正在成功获取movieID。我想插入那个movieDB ID到各自的电影。

请告诉我我错在哪里。

+0

首先,您使用的串插,而不是准备。 (下一个最好的事情是使用转义)。如果由于它不是HTTPS并且您的网站代码位于此处,所以如果服务器最终处于MITM'd状态?此外,无论何时发布代码,都要完成。我们还没有得到'$ htmlShowTime'的线索。另外,Whitespace是免费的。用它! – Amelia 2013-05-04 15:14:55

+0

@Hiroto它只是代码的一部分,它正在工作。我已经添加了一个新的部分来添加代码中的movieID列。 – Yogus 2013-05-04 15:22:53

回答

2

试试这个:

<?php 

$sql = ""; 
foreach ($arrTitle as $movie_name) { 
    $xml = simplexml_load_file("http://api.themoviedb.org/2.1/Movie.search/en/xml/myapi/$movie_name"); 
    foreach ($xml->movies->movie as $movies) { 
     $arrMovie_id = $movies->id; 
    } 
    $movie_ids = $arrMovie_id[0]; 
    $arrStr = explode(':', $htmlShowTime); 
    $release = substr($arrStr[3], 0, strlen($arrStr[3]) - 8); 
    $director = substr($arrStr[5], 0, strlen($arrStr[5]) - 11); 
    $sql .= "('$movie_name','null','$cast','$release','$director',250,230,'$movie_ids'),"; 
} 
$sql  = substr($sql, 0, -1); 
$sql_movie = "INSERT INTO `jos_movie`(`movie_name`,`language`,`cast`,`movie_release`,`director`,`rating`,`rating_count`,`movie_ids`) VALUES" . $sql; 

?> 
+0

它只是整个代码的一部分。如果你看到我获得电影名称。我在xml中添加电影名称,以便我可以将movieDb movieIds附加到它。 – Yogus 2013-05-04 15:18:34

+0

我打开了原始数组。在foreach结尾处,您现在可以在一行中完成整个查询。 – 2013-05-04 15:24:43

+0

谢谢@David我回应了$ movieids,它显示44425 44425.没有一个id对于movie_name数组中的任何电影都是正确的,并且它回显了两次:( – Yogus 2013-05-04 15:36:33