2014-09-27 60 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