2012-11-22 85 views
2

我試圖用xpath刪除一些數據,然後插入到SQL中。將循環數據插入到sql中

回聲工作正常,但插入到SQL時,它只獲取第一行。

例子:

<?php 
    $res=mysql_query("SELECT table.title, table.link FROM table WHERE table.link LIKE 'http://www.%' AND streams.title = ''"); 
    while($row=mysql_fetch_array($res)) 
    { 
     $html = new DOMDocument(); 
     @$html->loadHtmlFile($row["link"]); 
     $xpath = new DOMXPath($html); 

     foreach ($xpath->query("//table[@style='margin-left: 5px;']//td[@width='150']//a") as $files) 
     { 
     $html = new DOMDocument(); 
     @$html->loadHtmlFile($files->getAttribute('href')); 
     $xpath = new DOMXPath($html); 

     $hl = $xpath->query("//div[@style='width:742px']/a[preceding-sibling::div[@id='help1']]/@href")->item(0)->nodeValue; 
     $link=serialize(array('link' => $hl)); 
     $link=sqlesc($link); 
     echo $link; // all lines gets printed, thats okay? 
     mysql_query("UPDATE streams SET streams.hl=$link") or die(mysql_error()); // Only first line gets inserted, why? 
     } 

    } 
    ?> 
+0

您的UPDATE statment沒有WHERE clausule

,這聽起來像一個問題... –

+0

我也試過與WHERE clausule,仍然有問題。 – user1846022

回答

0

要更新所有行,你會得到包含相同的數據,因爲上次更新更新所有行的所有行,你想更新現有的行或剛剛插入新行?如果你想插入嘗試這種

INSERT INTO streams (hl) VALUES ($link) 

如果表中的其他非可選列,請將這些值也