2014-09-27 24 views
-1

你好,我正在使用下面的代碼來取消一些內容。 我需要插入到MySQL。此外,我必須檢查標題,如果是相同的,如果它不插入帖子。簡單的html dom和mysql插入

我的代碼在下面插入第一條記錄,即使它存在。

foreach($html->find('div.article_item') as $div) { 

     $title = $div->find('h2.title a ', 0)->innertext;    
     $thumb = $div->find('p.image_left img ', 0)->src; 
     $details = $div->children(3)->plaintext; 
     $url = $div->find('p.more a', 0)->href; 

     //I need only 3 posts to grab 
     for($i= 0 ; $i <= 3 ; $i++) 
     {  

     $qry="SELECT * FROM posts WHERE posttitle ='$title'"; 
     $result=mysqli_query($connectiondb, $qry); 

     //Check whether the query was successful or not 
     if($result) { 
     if(mysqli_num_rows($result) == 1) { 
     //Post Exists 
     }else { 

     $posttitle = $title; 
     $postthumb = $thumb; 
     $postdetails = $details; 
     $posturl = $url; 

     mysqli_query ($connectiondb, "INSERT INTO posts (posttitle, postthumb, postdetails, posturl) VALUES ('$posttitle', '$postthumb','$postdetails','$posturl')"); 

    } 


    }else { 
    (mysqli_error()); 
    } 
    mysqli_close($connectiondb); 

    } 


// Clear dom object 
$html->clear(); 
unset($html); 
} 
+0

那麼難看縮進和這麼多的SQL注入......不好 – 2014-09-27 00:11:10

+0

而究竟是什麼你的問題? – derdida 2014-09-27 00:12:02

+0

好的在你的oppinion是醜陋的..請至少解釋爲什麼它很醜,我怎樣才能讓它更安全?我不是職業球員,我正在努力! – 2014-09-27 00:24:14

回答

1

嘗試在這種情況下,改變== 1> 0

if(mysqli_num_rows($result) > 0) { 
    // Post Exists 
} else { 

而且由於你使用的mysqli,爲什麼不使用準備好的語句。

旁註:我看到的評論,我只需要三個職位,

+0

嘿Ghost ... Hello Life Savior ... :-)你可以在這裏打開一個聊天室,這樣我們可以分解它嗎? – 2014-09-27 00:28:24

+0

@IreneT。那麼在嘗試這些代碼後發生了什麼?你沒有得到任何錯誤?我不知道如何打開聊天,它只是彈出自己 – Ghost 2014-09-27 00:31:58

+0

我不能說它需要更多的聲譽...是否有可能解釋在哪裏excactly把你的代碼?試試這個:http://www.chattory.com/?chat=205786 – 2014-09-27 00:37:07